Android开发——Android M(6.0) 权限解决方案
自从Android M(6.0)发布以来,权限管理相比以前有了很大的改变,很多程序员发现之前运行的好好的Android应用在Android M上无法运行。本文现在把Android M中经常遇到的权限问题总结出来,并且给出相应的解决方案,分享给Android程序员兄弟们,希望给他们的开发工作带来帮助。
有一篇全面介绍Android M 运行时权限文章写的非常全面(翻译的):Android M 新的运行时权限开发者需要知道的一切,但是实施过程中还是遇到一些坑。
坑一:用Android5.0编译的apk,在Android6.0上运行完全没有问题。
在Android6.0以上才需要在运行时请求权限,在旧Android版本上保留原有逻辑,安装时授予权限。
用旧版本sdk编译的apk,都使用旧版本权限方式,安装时授予权限。(也就是说:兼容旧版本)
用Android6.0(targetSdkVersion 23)编译apk才需要处理新的权限问题。
还有一个蛋疼的问题:程序运行当中,用户关闭了权限,会发生什么?(还未知)
坑二:蓝牙扫描需要位置权限
BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
如何申请一个权限:用ACCESS_COARSE_LOCATION举个栗子
1. Manifest添加权限
Android6.0分了几种权限:Normal Permissions(安装时自动授权,用户也不能取消权限)(链接需要板凳) and Dangerous Permissions(链接需要板凳)
<!-- Android6.0 蓝牙扫描才需要--><uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- 1
- 2
- 1
- 2
2. 请求权限
//判断是否有权限// Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
//请求权限
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
//判断是否需要 向用户解释,为什么要申请该权限
ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)
//权限申请结果
onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
坑三:Fragment请求权限
//如果使用ActivityCompat.requestPermissions,不会调用onRequestPermissionsResult()//请求权限
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
//判断是否需要 向用户解释,为什么要申请该权限
shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
坑四:WRITE_SETTINGS权限怎么处理
android.permission.WRITE_SETTINGS不能自动授权,也不能运行时请求授权,咋整啊?通过打开Intent来让用户设置。貌似SETTINGS的权限只能这么处理,from CommonsWare research Android 6.0 changes.
/**
* An app can use this method to check if it is currently allowed to write or modify system
* settings. In order to gain write access to the system settings, an app must declare the
* {@link android.Manifest.permission#WRITE_SETTINGS} permission in its manifest. If it is
* currently disallowed, it can prompt the user to grant it this capability through a
* management UI by sending an Intent with action
* {@link android.provider.Settings#ACTION_MANAGE_WRITE_SETTINGS}.
*
* @param context A context
* @return true if the calling app can write to system settings, false otherwise
*/if(!Settings.System.canWrite(this)){
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
}
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (Settings.System.canWrite(this)) {
//检查返回结果
Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "WRITE_SETTINGS permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
其他类似文章参考:
1.探索新的Android权限模式
2.Android M 新的运行时权限开发者需要知道的一切
原文Everything every Android Developer must know about new Android’s Runtime Permission