标签:ext block get prompt action rom lin turn 请求
原文网址:https://yanlu.me/android-m6-0-permission-chasm/
有一篇全面介绍Android M 运行时权限文章写的非常全面:Android M 新的运行时权限开发者需要知道的一切,但是实施过程中还是遇到一些坑。
1
|
BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results |
Android6.0分了几种权限:Normal Permissions(安装时自动授权,用户也不能取消权限) and Dangerous Permissions
1
2
|
<!-- Android6.0 蓝牙扫描才需要--> < uses-permission-sdk-23 android:name = "android.permission.ACCESS_COARSE_LOCATION" /> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//判断是否有权限 // Here, thisActivity is the current activity if (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
|
//如果使用ActivityCompat.requestPermissions,不会调用onRequestPermissionsResult() //请求权限 requestPermissions( new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION); //判断是否需要 向用户解释,为什么要申请该权限 shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS) |
android.permission.WRITE_SETTINGS不能自动授权,也不能运行时请求授权,咋整啊?通过打开Intent来让用户设置。貌似SETTINGS的权限只能这么处理,from CommonsWare research Android 6.0 changes.
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
29
|
/** * 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); } @Override protected 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(); } } } |
GitHub地址:Android_M_requestPermissions
标签:ext block get prompt action rom lin turn 请求
原文地址:http://www.cnblogs.com/wi100sh/p/6207362.html