在我的代码中,我要求:
int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE);
int permissionCheck1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS);
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_PHONE_STATE}, 0);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS}, 2);
我还包括:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
// 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.
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_PHONE_STATE}, 0);
}
return;
}
case 1: {
// 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.
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
}
return;
}
case 2: {
// 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.
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS}, 2);
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
运行应用程序时,我首先收到请求,然后在不触摸任何内容的情况下,我的应用程序崩溃,并出现以下错误:
FATAL EXCEPTION: main Process: com.example.ortel.tagnet, PID: 28445 java.lang.StackOverflowError: stack size 8MB at com.example.ortel.tagnet.MainActivityOld.onRequestPermissionsResult(MainActivityOld.java:894) at android.app.Activity.requestPermissions(Activity.java:4455) at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507) at com.example.ortel.tagnet.MainActivityOld.onRequestPermissionsResult(MainActivityOld.java:894) at android.app.Activity.requestPermissions(Activity.java:4455) at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507) at com.example.ortel.tagnet.MainActivityOld.onRequestPermissionsResult(MainActivityOld.java:894) at android.app.Activity.requestPermissions(Activity.java:4455) at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507) at com.example.ortel.tagnet.MainActivityOld.onRequestPermissionsResult(MainActivityOld.java:894)
对于您的信息,(MainActivityOld.java:894)是
ActivityCompat.requestPermissions(此为新 String [] {android.Manifest.permission.READ_PHONE_STATE},0);
发生了什么事?看来它一直在打电话给我。
它不应该等待我的回答吗?
编辑: 。如果您不知道为什么会发生,请说明。
答案 0 :(得分:0)
您可以尝试一次调用Activity.requestPermissions来请求所有权限,并输入具有您需要询问的所有3个权限的String [],在您的代码中,您实际上是在通过3种不同的对话框提示系统要求这些权限作为单独的实体,也许在执行第二个权限请求时,它会覆盖第一个权限请求,并创建一个生成StackOverflowException的循环。
示例:
List <String> str = new ArrayList <> ();
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
str.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
str.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
str.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
str.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
str.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (str.size() > 0) {
String[]strings = new String[str.size()];
str.toArray(strings);
ActivityCompat.requestPermissions(StartActivity.this,
strings,
PERMISSION_CODE_STATE);
}
然后在接收结果的回调中检查您是否收到了所有类似的权限,或者手动检查每个grantResult: boolean gotPermissions = true;
for (int i : grantResults) {
if (i != PackageManager.PERMISSION_GRANTED) {
gotPermissions = false;
break;
}
}
答案 1 :(得分:0)
尝试这个。
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
...add your permissions.
};
在您的onCreate中添加它。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}
这是您的权限功能。
public boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
此代码将一步一步检查您的所有权限。但是如果有人拒绝了许可。期望您的应用会在需要该权限时崩溃。
答案 2 :(得分:0)
请参见this answer。
您遇到一个无限循环,我猜这是由于有人拒绝“位置”权限而导致的,并选中了“永不询问”复选框