我尝试检查权限 在我的清单中我设置了
<uses-permission android:name="android.permission.READ_CONTACTS" />
然后在片段中我尝试检查
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_CONTACTS);
但是,尽管操作系统中的应用程序属性具有任何权限,但我只获得0。 我检查了Android 4.4.4 MIUI 6.5.1 可能是MIUI的原因
如果我尝试在Manifest中没有查看权限,我有-1,但我想知道权限的切换状态。
感谢名单
答案 0 :(得分:2)
将此函数添加到您的代码中,并仅在MIUI中为READ_COntacts调用checkUsagePermission()
private boolean checkUsagePermission() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = 0;
mode = appOps.checkOpNoThrow("android:read_contacts", android.os.Process.myUid(), getPackageName());
boolean granted = mode == AppOpsManager.MODE_ALLOWED;
if (!granted) {
//write your code here for accept that permission
return false;
}
}
return true;
}
答案 1 :(得分:1)
0表示PackageManager.PERMISSION_GRANTED
Android 6中的Android版本为零,因为在安装应用之前已经提供了权限。如果您在运行Android 6或更高版本的手机中安装此应用程序,则它将返回-1,这意味着PackageManager.PERMISSION_DENIED,因为Android 6在安装应用程序之前不会要求权限,而是在运行时请求权限。
因此写这样的代码 -
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_DENIED)
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
PERMISSION_REQUEST_CODE);
这样,只有在您没有权限的情况下,您的应用才会要求获得许可,并且只会在Android 6及更高版本上发生。在Android 6下面,您已经获得了许可。
编辑:似乎MIUI在Google在Android 6中引入它们之前提供了运行时权限。因此,您面临的问题是MIUI特有的。如果您在Android 6下使用MIUI以外的任何其他设备,则您将无法选择拒绝选择性权限。您要么提供所有权限,要么取消安装。所以你的应用程序将在所有其他设备上正常工作。
答案 2 :(得分:1)
我刚刚使用MIUI 7 6.3.31 Beta测试MI 4W。
我正面临同样的checkSelfPermission返回true错误,就像我只请求Manifest.permission.READ_CONTACTS时那样。
即使我拒绝了Manifest.permission.WRITE_CONTACTS的请求,它也会将“拒绝”设置为Manifest.permission.READ_CONTACTS。检查这两个权限将返回true。
虽然我已经拒绝,但checkSelfPermission(Manifest.permission.READ_CONTACTS)返回true,偶尔MIUI会弹出一条消息,通知用户对CONTACTS的访问被拒绝。
以下是我的实际权限: checkSelfPermission(Manifest.permission.READ_CONTACTS)
PermissionUtil:
public static boolean hasSelfPermission(Context context, String permission) {
// Below Android M all permissions are granted at install time and are already available.
if (!isMNC()) {
return true;
}
return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}
public static boolean isMNC() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
测试代码:
Log.d(TAG, "READ_CONTACTS: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.READ_CONTACTS));
Log.d(TAG, "WRITE_CONTACTS: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.WRITE_CONTACTS));
Log.d(TAG, "ACCESS_FINE_LOCATION: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.ACCESS_FINE_LOCATION));
Log.d(TAG, "ACCESS_COARSE_LOCATION: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION));
Log.d(TAG, "CAMERA: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.CAMERA));
Log.d(TAG, "RECORD_AUDIO: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.RECORD_AUDIO));
Log.d(TAG, "READ_EXTERNAL_STORAGE: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE));
Log.d(TAG, "WRITE_EXTERNAL_STORAGE: " + PermissionUtil.hasSelfPermission(TestActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE));
“应用设置”中拒绝了所有权限。
结果:
04-01 13:16:54.967 25481-25481/com.test.test D/TestActivity: READ_CONTACTS: true
04-01 13:16:54.967 25481-25481/com.test.test D/TestActivity: WRITE_CONTACTS: true
04-01 13:16:54.967 25481-25481/com.test.test D/TestActivity: ACCESS_FINE_LOCATION: false
04-01 13:16:54.968 25481-25481/com.test.test D/TestActivity: ACCESS_COARSE_LOCATION: false
04-01 13:16:54.968 25481-25481/com.test.test D/TestActivity: CAMERA: false
04-01 13:16:54.969 25481-25481/com.test.test D/TestActivity: RECORD_AUDIO: false
04-01 13:16:54.969 25481-25481/com.test.test D/TestActivity: READ_EXTERNAL_STORAGE: false
04-01 13:16:54.970 25481-25481/com.test.test D/TestActivity: WRITE_EXTERNAL_STORAGE: false
我在Nexus 5上进行了测试并拒绝了所有权限。
结果:
04-01 13:16:54.967 25481-25481/com.test.test D/TestActivity: READ_CONTACTS: false
04-01 13:16:54.967 25481-25481/com.test.test D/TestActivity: WRITE_CONTACTS: false
04-01 13:16:54.967 25481-25481/com.test.test D/TestActivity: ACCESS_FINE_LOCATION: false
04-01 13:16:54.968 25481-25481/com.test.test D/TestActivity: ACCESS_COARSE_LOCATION: false
04-01 13:16:54.968 25481-25481/com.test.test D/TestActivity: CAMERA: false
04-01 13:16:54.969 25481-25481/com.test.test D/TestActivity: RECORD_AUDIO: false
04-01 13:16:54.969 25481-25481/com.test.test D/TestActivity: READ_EXTERNAL_STORAGE: false
04-01 13:16:54.970 25481-25481/com.test.test D/TestActivity: WRITE_EXTERNAL_STORAGE: false
结束这是MIUI 7 6.3.31 Beta的错误。
答案 3 :(得分:0)
检查:
https://developer.android.com/reference/android/content/pm/PackageManager.html#PERMISSION_GRANTED
0代表PERMISSION_GRANTED
我在 Github 上创建了一个示例应用,以演示权限模型。
/**
* Check build version
*/
if(Build.VERSION.SDK_INT < 23){
displayContacts();
}else {
requestContactPermission();
}
private void requestContactPermission() {
int hasContactPermission = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS);
if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, READ_CONTACT_REQUEST_CODE);
}else {
Toast.makeText(MainActivity.this, "Contact Permission is already granted", Toast.LENGTH_LONG).show();
displayContacts();
}
}
此处,READ_CONTACT_REQUEST_CODE
是任何整数值
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case READ_CONTACT_REQUEST_CODE:
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Log.i("Permission", "Contact permission has now been granted. Showing result.");
Toast.makeText(MainActivity.this,"Contact Permission is Granted",Toast.LENGTH_SHORT).show();
displayContacts();
} else {
Log.i("Permission", "Contact permission was NOT granted.");
Toast.makeText(MainActivity.this,"Permission is not Granted",Toast.LENGTH_SHORT).show();
}
break;
}
}