Android6.0 Run-time permissions

Source: Internet
Author: User

Starting with Android 6.0 (API level 23), users are granted permissions when they use the software, rather than authorizing it at installation time. This method simplifies the installation process, and users do not need to grant permissions when installing or updating the software. It also enables users to control the functionality of the application. For example a photographic application, his use of camera permissions is normal, but users do not understand the application to read the contact information.

System permissions fall into two categories: Normal permissions and Dangerous permissions
    • Normal permissions do not directly pose a risk to user privacy. Register in the Androidmanifest.xml.
    • Dangerous permissions grant the app permission to access the user's confidential data. Dangerous permissions are registered in Androidmanifest.xml, and users are required to authorize your software to use this permission.

Known as what belongs to the normal rights, which are dangerous rights, we go to the official website to read.

https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
Simple Example (6.0 mobile phone access to dangerous rights)

If our app needs a call-to-phone feature, this time we need to get Call_phone permissions. Let's take a look at how to write the phone code on a 6.0 phone:

 Public  class mainactivity extends appcompatactivity implements View . Onclicklistener {    PrivateString[] perms = {Manifest.permission.CALL_PHONE};Private Final intPerms_request_code = $;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Findviewbyid (R.id.btn_call_phone). Setonclicklistener ( This); }@Override     Public void OnClick(View v) {if(V.getid () = = R.id.btn_call_phone) {//Get permission to make a call            if(Build.VERSION.SDK_INT > Build.version_codes. LOLLIPOP_MR1) {//android 6.0 or later requires permissionRequestpermissions (Perms,perms_request_code);//Request Permission}Else{Callphone (); }        }    }/** * Get permission callback method * @param permsrequestcode * @param permissions * @param Gran Tresults * *    @Override     Public void Onrequestpermissionsresult(intPermsrequestcode, string[] permissions,int[] grantresults) {Switch(Permsrequestcode) { CasePerms_request_code:Booleanstorageaccepted = grantresults[0] = = packagemanager.permission_granted;if(storageaccepted)                {Callphone (); }Else{LOG.I ("Mainactivity","no permission to operate this request"); } Break; }    }// Call    Private void Callphone() {//Check call Permissions        if(Activitycompat.checkselfpermission ( This, Manifest.permission.CALL_PHONE) = = packagemanager.permission_granted) {Intent Intent =NewIntent (Intent.action_call); Intent.setdata (Uri.parse ("Tel:"+"10086"));        StartActivity (Intent); }    }}

Call the Requestpermissions method to request permission before obtaining permission to determine the version number and version greater than 6.0. This method has two parameters, parameter one is the permission array to request, can request more than one permission at a time. Parameter 2 is a request identifier of type int. The Onrequestpermissionsresult method is recalled regardless of user consent or rejection. The Permsrequestcode parameter of this method is the second argument on our request, and then the first parameter of the Permsrequestcode array is judged. The value equals packagemanager.permission_granted is the user agrees to authorize. Otherwise, the user refuses to authorize this permission.

Get Special Permissions

There are only two types of Android special permissions: System_alert_window and Write_settings,
Unlike normal permissions and dangerous permissions. Special permissions are more sensitive, so most apps should not use them. If an app requires one of these permissions, it must be declared in the manifest and sent to the requesting user's authorized intent. A detailed management screen is displayed to the user in response to the intent.

First we determine whether the Android version number is greater than 6.0, through settings.candrawoverlays to determine if there is no system pop-up window permissions. If no permission is sent, a intent request authorization is requested.

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1){    if (Settings.canDrawOverlays(this)) {        systemAlert();    } else {//没有系统弹窗权限,发送一个设置权限的intent        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);        intent.setData(Uri.parse("package:" + getPackageName()));        startActivityForResult(intent, PERMS_REQUEST_CODE);    }}

Overriding the Onactivityresult method, authorization failure or success will callback this method. Here to determine whether the user agrees or refuses.

@Overrideprotectedvoid Onactivityresult (int requestcode, int resultcode,Intent Data){if(Requestcode==Perms_request_code) {if(Build.VERSION.Sdk_int>=Build.Version_codes.M) { //Android MProcessingRuntime Permission            if(Settings.candrawoverlays( This) {//have system popup permissionsLog.I("mainactivity"," Get system popup (special permission) successfully ");Systemalert(); }Else{ Log.             I("mainactivity", "Deny system popup (special Permission)"); }}    }}
Wouldn't it be tiring to apply for so many privileges?

In fact, you do not need to apply for each permission, for example, if your app authorizes the access to read contacts, then your app is also given the right to write to the contact. Because both permissions for reading contacts and writing to contacts are grouped by contact permissions, other permissions for that group are allowed once a permission is allowed in the group.

Attention

Run-time permissions are requested in your code, but you must remember registration permissions in the Manifest.xml file.

<uses-permission android:name="android.permission.CALL_PHONE"/><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
As follows

SOURCE download

SOURCE download

Android6.0 Run-time permissions

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.