私はMarshmallow以上のデバイスで実行時にアンドロイドアプリのアクセス権を求めています。アクセス許可は適切に付与されますが、アプリを終了するとすぐに(強制停止ではなくバックグラウンドから削除してください)、アクセス権がなくなり、再びアプリケーションを開いたときにアクセス権が再度要求されます。アプリがバックグラウンドから削除されたときにアクセス権が失われました
boolean granted=checkPermission();
Log.d("Permission already there","---"+granted);// it always returns false
if (Build.VERSION.SDK_INT >= 23) {
System.out.println("Build version is"+Build.VERSION.SDK_INT);
if(!granted){
requestPermission();
}
}
private void requestPermission() {
ActivityCompat.requestPermissions(activity,new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.GET_ACCOUNTS,
},RequestPermissionCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean ACCESS_FINE_LOCATION = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean RECORD_AUDIO = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean CALL_PHONE = grantResults[2] == PackageManager.PERMISSION_GRANTED;
boolean CAMERA = grantResults[3] == PackageManager.PERMISSION_GRANTED;
boolean WRITE_EXTERNAL_STORAGE = grantResults[4] == PackageManager.PERMISSION_GRANTED;
boolean GET_ACCOUNTS = grantResults[5] == PackageManager.PERMISSION_GRANTED;
boolean READ_CONTACTS = grantResults[6] == PackageManager.PERMISSION_GRANTED;
if (ACCESS_FINE_LOCATION && RECORD_AUDIO && CALL_PHONE && CAMERA && WRITE_EXTERNAL_STORAGE && GET_ACCOUNTS && READ_CONTACTS) {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int FirstPermissionResult= ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
int SecondPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO);
int ThirdPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE);
int FourthPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
int FifthPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int SixthPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS);
int SeventhPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS);
return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
FourthPermissionResult == PackageManager.PERMISSION_GRANTED &&
FifthPermissionResult == PackageManager.PERMISSION_GRANTED &&
SixthPermissionResult == PackageManager.PERMISSION_GRANTED &&
SeventhPermissionResult == PackageManager.PERMISSION_GRANTED;
}
私はすべての権限を付与していますが、アプリケーションを再オープンするたびにfalseを返します。 – Kalpit
リストから削除する必要があるアクセス許可の一覧を追加しました。 –
https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/enこれも読んでください –