"Go" Android M (6.0) Privilege Crawl Pit Tour

Source: Internet
Author: User

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.
    1. 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.
    2. 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)
    3. New permissions issues need to be addressed with Android6.0 (Targetsdkversion 23) as the target version.
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.