Java onRequestPermissionsResult未调用
运行playstore Build Apk时未调用Marshmallow Runtime Permission onRequestPermissionsResult,但正常调试Apk时运行良好。有人帮助我吗..谢谢 这是我的密码 OnRequestPermissionResult方法Java onRequestPermissionsResult未调用,java,android,Java,Android,运行playstore Build Apk时未调用Marshmallow Runtime Permission onRequestPermissionsResult,但正常调试Apk时运行良好。有人帮助我吗..谢谢 这是我的密码 OnRequestPermissionResult方法 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { try
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
try {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_RECORD: {
// 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.
ChatActivity();
} else {
Toast.makeText(MainActivity.this, "Permission denied Chat ", Toast.LENGTH_SHORT).show();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
} catch (Exception e) {
Log.e(Constants.LOG, e.getMessage());
}
}
这是正确的做法。我在做下面基本相同的事情 下面是一个关于我如何使其适用于我的代码的示例
private void requestStoragePermission() {
/*
We don't have permission so prompt the user
If permission is not granted control goes to onRequestPermissionResult
*/
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
onRequestPermissionsResult方法:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("PERMISSION", "Storage permission granted");
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOkCancel("You need to allow access to Storage to use the offline timetables",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
requestStoragePermission();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(MainActivity.this, "Storage permission required to continue", Toast.LENGTH_LONG)
.show();
finishAffinity();
break;
}
}
});
} else {
Toast.makeText(this, "Go to settings and enable storage permissions", Toast.LENGTH_LONG)
.show();
finishAffinity();
}
}
}
}
showMessageOkCancel方法:
private void showMessageOkCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
说明:
在主代码中,您只请求StoragePermission()
无论是否授予权限,onRequestPermissionsResult都将运行
如果许可被授予,那就好了,仅此而已
如果未授予权限,则如果是第一次请求权限,shouldShowRequestPermissionRegulation()将返回true。然后,我会提示另一个对话框,并给出一些解释,让用户有机会再次启用该权限
但是,如果选中了“不再询问”,则shouldShowRequestPermissionRegustion将返回false。requestPermissions对话框将不再弹出。用户必须转到手动启用权限
注意,您不必使用finishefinity()。您只需禁用需要该权限的代码部分。它仅适用于棉花糖及以上版本。较低版本将忽略此请求…是的,我正在测试nexus 5 marshmallow 6.1,我也在使用上述方法,但我在未调用RequestPermissionResult的活动和片段活动中遇到了相同的问题。EnablePermissions()应仅具有我在requestStoragePermission()中所做的操作。代码的其余部分在onRequestPermissionsResult()中。确保在调用EnablePermissions()的活动中启用了RequestPermissionsResult()。然后在代码中只调用EnablePermissions(),它应该可以工作。。。如果它没有显示你的代码
private void showMessageOkCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}