在Android权限检查中我正在检查 onResume()中的相机权限,但是当 -
停用该应用的相机权限
当用户回来时,app不会要求相机许可。
答案 0 :(得分:0)
经过大量搜索后找到答案 -
如果您在应用运行时更改权限,则无法回拨(设置 - >应用 - >权限 - >关闭权限)
但它会再次调用onCreate方法。因此,您需要检查 onCreate()
中的权限答案 1 :(得分:-1)
为扩展Activity
的权限创建抽象类:
public abstract class RuntimePermission extends Activity {
private SparseIntArray mErrorString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mErrorString = new SparseIntArray();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for(int permission: grantResults){
permissionCheck = permissionCheck + permission;
}
if(grantResults.length > 0 && PackageManager.PERMISSION_GRANTED == permissionCheck){
onPermissionsGranted(requestCode);
}
else{
Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:"+getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
}
}).show();
}
}
public abstract void onPermissionsGranted(int requestCode);
public void requestAppPermission(final String[] requestedPermissions, final int stringId, final int requestCode) {
mErrorString.put(requestCode, stringId);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
boolean showRequestPermissions = false;
for (String permission : requestedPermissions) {
permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
showRequestPermissions = showRequestPermissions || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (showRequestPermissions) {
Snackbar.make(findViewById(android.R.id.content), stringId, Snackbar.LENGTH_INDEFINITE).setAction("YES", new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(RuntimePermission.this, requestedPermissions, requestCode);
}
}).show();
} else {
ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
}
}
else{
onPermissionsGranted(requestCode);
}
}
}
然后由你的类扩展这个抽象类:
public class YourClass extends RuntimePermission {
private static final int REQUEST_PERMISSION = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_class_activity);
}
@Override
protected void onResume() {
super.onResume();
requestAppPermission(new String[]
{
Manifest.permission.CAMERA
},
R.string.approve_permissions, REQUEST_PERMISSION); //R.string.approve_permissions is your text for snackbar
}
@Override
public void onPermissionsGranted(int requestCode) {
// your logic
}
}