在Android 6.0及更高版本中,在运行时授予的权限,用户可以通过转到设置来取消授予的权限。
我希望在用户通过转到设置取消授予的权限后立即运行一些代码。反正有吗?
答案 0 :(得分:0)
//调用此方法以检查其他活动或功能之前的权限//调用
if (hasPermission()) {
// do thing
} else {
requestPermissionDialog();
}
/////////////////调用hasPermission //////////////////////////// < / p>
private boolean hasPermission() {
boolean permission = true;
if (ActivityCompat.checkSelfPermission(WelcomeActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
permission = false;
}
return permission;
}
////////////////////////// requestPermissionDialog //////////////////// ///
private void requestPermissionDialog() {
permissionStatus = getSharedPreferences("permissionStatus", MODE_PRIVATE);
if (ActivityCompat.checkSelfPermission(WelcomeActivity.this, Manifest.permission.WRITE_Eenter code hereXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show Information about why you need the permission
// resetPasswordDialog(WelcomeActivity.this,"ok");
AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
builder.setInverseBackgroundForced(true);
builder.setTitle("Storage Permission");
builder.setMessage("This app needs Storage permission.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE, false)) {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
// resetPasswordDialog(WelcomeActivity.this,"settings");
AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
builder.setInverseBackgroundForced(true);
builder.setTitle(" Storage Permission");
builder.setMessage("This app needs Storage permission.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
Toast.makeText(getBaseContext(), "Go to Permissions to allow Storage Audio ", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
//just request the permission
ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE, true);
editor.commit();
}
}