Original URL: https://yanlu.me/android-m6-0-permission-chasm/
There is a comprehensive introduction to the Android M runtime permissions article written very comprehensive: Android m new runtime permissions developers need to know everything, but the implementation process still encountered some pits.
Pit one: With Android5.0 compiled APK, run on Android6.0 completely no problem.
- You need to request permissions at run time over Android6.0, leave the original logic on the old Android version, and grant permissions when you install.
- The old version of the SDK compiled APK, both use the old version of permissions, the installation to grant permissions. (That is, older versions are compatible)
- New permissions issues need to be addressed with Android6.0 (Targetsdkversion 23) as the target version.
- There is an egg ache problem: In the program running, the user closed the permissions, what happens? (also unknown)
Pit Two: Bluetooth scanning requires location permissions, and positioning to open
| 1 |
BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results |
How to apply for a permission: Raise chestnuts with access_coarse_location 1. Manifest Adding permissions
Android6.0 has several permissions: Normal Permissions (Automatic authorization at installation, user cannot cancel permissions) and dangerous Permissions
| 12 |
<!-- Android6.0 蓝牙扫描才需要--><uses-permission-sdk-23android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
2. Request permission
| 12345678910111213141516 |
//判断是否有权限// 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) |
Pit Three: Fragment request permission
| 123456 |
//如果使用ActivityCompat.requestPermissions,不会调用onRequestPermissionsResult()//请求权限requestPermissions(newString[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);//判断是否需要 向用户解释,为什么要申请该权限shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS) |
Pit Four: Write_settings permissions how to deal with
Android.permission.WRITE_SETTINGS can not be automatically authorized, and can not be run when the request authorization, why the whole ah? Let the user set up by opening intent. Seemingly settings permission can only be handled so, from CommonsWare the Android 6.0 changes.
| 1234567891011121314151617181920212223242526272829 |
/** * 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(); } }} |
Source Address
GitHub Address: Android_m_requestpermissions
"Go" Android M (6.0) Privilege Crawl Pit Tour