"Android Runtime permissions request Quick Learning Tutorial"

Source: Internet
Author: User

1. Introduction to Android rights

Android permissions are the power that Android apps need to run on the device, and the obligation is to bring the functionality of this Android app to the user.

Source: Before the Android6.0 version, according to the app in the Androidmanifest request permission, when installing the app to prompt permission to allow, do not allow the use of no, then we can only silently endure this app to our privacy erosion (Eg. Read access to the Address book).

Workaround: Google later discovered that, alas, this would cause Android users to want to use some of the features of the app and not be able to use them. Therefore, in Android6.0 the permissions are divided into two categories, that is, normal Permissions (normal permissions) and dangerous Permission (dangerous permissions), the app at the time of the operation of the required permissions can be dynamic application.

2. Android Runtime permission request (based on easypermissions)

2.1 Questions:

Request camera and Access_fine_location permissions on the Android 6.0 version based on Easypermissions.

2.2 Problem Solving Process:

I. Add the Onrequestpermissionsreult () Request permission function to the activity that uses the permission.

 Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) {        Super. Onrequestpermissionsresult (Requestcode, permissions, grantresults); //Forward results to easypermissionsEasypermissions.onrequestpermissionsresult (Requestcode, permissions, Grantresults, This); }}

Ii. custom Methodrequirestwopermission () functions that require run-time permissions.

@AfterPermissionGranted (rc_camera_and_location)Private voidmethodrequirestwopermission () {string[] perms={Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION}; if(Easypermissions.haspermissions ( This, perms)) {//1, first check if this permission exists//already has permission, do the thing// ...}Else {        //Do not has permissions, request them nowEasypermissions.requestpermissions ( This, GetString (R.string.camera_and_location_rationale), rc_camera_and_location, perms); 2. No permission is requested}}

III. Convenient debugging, whether the user is authorized or refused to decide which permission can rewrite activity's onpermissionsgranted and onpermissionsdenied functions.

@Override Public voidOnpermissionsgranted (intRequestcode, list<string>list) {
     //Requestcode is just rc_camera_and_location //Some permissions have been granted// ...} @Override Public voidOnpermissionsdenied (intRequestcode, list<string>list) {
//Some permissions have been denied
     //...
  }

3. Summary

The first step, in order to be compatible with the Android6.0 version of the rights to apply, we still need to declare in androidmanifest we need the authority;

Second, the Android6.0 version of the run-time permission request is used based on easypermissions for dynamic applications.

In addition, one of the permissions that belong to the same permission group is authorized, and the remaining permissions within the permission group are authorized.

4. Reference:

Https://developer.android.com/guide/topics/security/permissions.html?hl=zh-cn#normal-dangerous

http://blog.csdn.net/lmj623565791/article/details/50709663

Https://github.com/googlesamples/easypermissions

Http://mp.weixin.qq.com/s/OQRHEufCUXBA3d3DMZXMKQ

"Android Runtime permissions request Quick Learning Tutorial"

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.