連絡先の詳細のアクセス/カレンダーアクセスなどのためにユーザーが「許可」ボタンを押したときにイベントを識別できる方法があるかどうかを知りたい場合はAndroidの許可:ユーザーが「許可」を押した後にタスクを実行
ActivityCompat.requestPermissionsで権限を要求する方法はありますが、ユーザーが権限を与えた直後にアクションを実行する方法があることは知っていますか?
連絡先の詳細のアクセス/カレンダーアクセスなどのためにユーザーが「許可」ボタンを押したときにイベントを識別できる方法があるかどうかを知りたい場合はAndroidの許可:ユーザーが「許可」を押した後にタスクを実行
ActivityCompat.requestPermissionsで権限を要求する方法はありますが、ユーザーが権限を与えた直後にアクションを実行する方法があることは知っていますか?
最初に変数を定義します。使用して
public static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
要求許可:
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
は現在使用して結果をキャッチ:
FRAGMENT FOR @Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION : {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
あなたはfragment
にこのコードをしようとしている場合は
、
checkSelfPermission()
ActivityCompact.checkSelfPermission()
を変更しても変更
ActivityCompat.requestPermissions()
許可結果の取り扱いrequestPermissions()
に(許可または拒否)の活動と同じです。より完全な例については
ありがとうございます@ rafsanahmad007これは私が探していたものです –
全く動作していません –
ビルドapkをアンインストールしてクリーンアップしてください...マニフェストにもパーミッションを追加してください...アンドロイドバージョンがmarsmallow未満の場合、 – rafsanahmad007
requestPermissions()
にアクセス権を要求してください。結果はonRequestPermissionsResult()
にあり、許可を受けているかどうかを判断できます。それはあなたのアプリのコードの観点から、「直後」です。
を参照してください。私は非常に目的のためにこのコードを使用していました。
public boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA
}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG, "Permission is granted");
return true;
}
}
あなたはその後、呼び出すことができます。
if(isPermissionGranted())
{
// do your stuff
}
それは私のアプリを絶えずクラッシュさせている –
はい。プロンプトの結果を返すコールバック関数 'onRequestPermissionsResult(..)'は、それ以上のコードを実行するために使用できます。 – Pztar