我正在尝试对Launcher Activity请求权限。对于API< 23,它完美无缺。但是,当我在运行API 23的设备上测试应用程序时,它说:“PostPaid Balance已停止。”我点击“关闭应用程序按钮”,应用程序关闭并立即请求一个权限。我接受了。然后我点击应用程序图标重新打开并发生同样的事情,除了现在它要求下一个权限。然后我点击应用程序图标,这次正确执行。 好像它一次要求一个权限。关于如何解决这个问题的任何想法?
// Below code is implemented on onCreate() of the launcher activity.
if (Build.VERSION.SDK_INT < 23) {
ActivityCompat.checkSelfPermission(this.getApplicationContext(), "android.permission.READ_SMS");
ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_CALL_LOG);
ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_PHONE_STATE);
if ((ActivityCompat.checkSelfPermission(this, "android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED)) {
requestSmsPermission();
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
requestPhoneStatePermission();
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
requestCallLogPermission();
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((this.checkSelfPermission("android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED) &&
(this.checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) &&
(this.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) {
this.requestPermissions(new String[]{"android.permission.READ_SMS", Manifest.permission_group.PHONE}, REQUEST_SMS);
}
}
答案 0 :(得分:2)
要回答有关如何一次请求多个权限的问题,请为您的字符串数组添加权限。在此示例中,我想要请求READ_PHONE_STATE和WRITE_EXTERNAL_STORAGE的权限:
ArrayList<String> arrPerm = new ArrayList<>();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
arrPerm.add(Manifest.permission.READ_PHONE_STATE);
}
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!arrPerm.isEmpty()) {
String[] permissions = new String[arrPerm.size()];
permissions = arrPerm.toArray(permissions);
ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}
现在,检查授予了哪些权限:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0) {
for(int i = 0; i < grantResults.length; i++) {
String permission = permissions[i];
if(Manifest.permission.READ_PHONE_STATE.equals(permission)) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
// you now have permission
}
}
if(Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
// you now have permission
}
}
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
break;
}
}
// other 'case' lines to check for other
// permissions this app might request
}