Detailed Android Rights Management Android 6.0 runtime permissions and Solutions _android

Source: Internet
Author: User

Objective:

Today it's still a hot topic around the recent interview. Android 6.0 right fit to sum up learning, in fact, Android 6.0 rights matching our company is only started this May, is relatively late, but now Android more than 6.0 devices more and more, So the Android 6.0 privilege is essential, and here's how our company does the Android 6.0 right fit.

Android 6.0 The following non-run-time permissions:

According to the above blog we know very clearly that the right to Android is in fact for more secure access to the program, so there is a hierarchy of permissions, such as: Normal Low-risk permissions, dangerous high risk permissions, and so on, although there is this security awareness, However, these permissions are only asked once during installation, and once installed, if the app requests high-risk permissions, and most users rarely pay attention to these permissions lists when they install, and many of the Android markets have silently installed features, users are less aware of any permission prompts. In this way, the app is likely to do some harm to the user in the background. As shown in the following illustration:

Android6.0 Run time permissions:

Given the relatively less secure version rights management prior to 6.0, Android 6.0 adopts a new permission model that only tells the user whether to authorize it at runtime time, rather than when it was originally installed, and by default every time the page is opened at run time, You need to check to see if you have the required permission request. Such users have a lot of autonomy, such as the user can give the app the right to camera, also can use permissions.

Android 6.0 rights fit:

1. The phenomenon caused by no adaptation

First look at the Build.gradle configuration of app module

 Compilesdkversion
 buildtoolsversion "24.0.2"
 defaultconfig {
  ApplicationID " Com.whoislcj.rxpermissions "
  minsdkversion
  targetsdkversion
  versioncode 1
  versionname" 1.0 "
 }

Because more than 6.0 of Android's permissions become runtime permissions, which means that you have to apply for it dynamically when you need to use a permission, direct access directly causes the app to crash.

2.) Early Solutions

In fact, to determine whether the need for run-time permissions of the tag is targetsdkversion, when the targetsdkversion<23, only when the installation of permissions, use will not be reminded, The new run-time permission rule is used when targetsdkversion≥23. All in the first encounter due to the power mismatch caused by the crash, our team is the solution is to targetsdkversion artificially down to less than 23, so that the default use of permissions, but this is not Google recommended use.

 Compilesdkversion
 buildtoolsversion "24.0.2"
 defaultconfig {
  ApplicationID " Com.whoislcj.rxpermissions "
  minsdkversion
  targetsdkversion
  versioncode 1
  versionname" 1.0 "
 }

3.) Determine whether you have permission to use this permission

Check to see if you have tenure

 public boolean isgranted (String permission) {return
  !ismarshmallow () | | isgranted_ (permission);
 }

Determine if Android is more than 6.0

 Private Boolean Ismarshmallow () {return
  Build.VERSION.SDK_INT >= build.version_codes. M;
 }

Whether you have requested the permission to use

 Private Boolean isgranted_ (String permission) {
  int checkselfpermission = Activitycompat.checkselfpermission (This , permission);
  return checkselfpermission = = packagemanager.permission_granted;
 

Contextcompat.checkselfpermission, which is primarily used to detect whether a permission has been granted, and the method return value is Packagemanager.permission_ Denied or packagemanager.permission_granted. When you return to denied, you need to apply for authorization.

4.) Request permission to use

private void Requestpermission (String permission, int requestcode) {
  if (!isgranted (permission)) {
   if ( Activitycompat.shouldshowrequestpermissionrationale (this, permission)) {

   } else {
    Activitycompat.requestpermissions (this, new string[]{permission}, Requestcode);
   }
  else {
   //directly perform the appropriate operation
  }
 }

Shouldshowrequestpermissionrationale is primarily used to give users an explanation of the application permissions that only the user has rejected in the last permission application. In other words, the user has rejected the once, you play an authorization box, you need to give the user an explanation, why to authorize, then use this method. Requestcode this needs to be in the processing of the callback when one by one corresponds.

5.) Processing Authorization callback

@Override public
 void Onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) {
  if (Requestcode = = CAMERA) {
   if (grantresults[0] = = packagemanager.permission_granted) {
    String Jpgpath = Getcach Edir () + "test.jpg";
    Takephotobypath (Jpgpath, 2);
   } else {
    //Permission Denied
    toast.maketext (mainactivity.this, "You do not authorize this permission, open authorization in Settings, Toast.length_short)." Show ();
   }
   return;
  }
  Super.onrequestpermissionsresult (Requestcode, permissions, grantresults);
 }

6. Full sample of the activity

public class Mainactivity extends Appcompatactivity {private static final int CAMERA = 2;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main); Findviewbyid (r.id.request_permission). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (
   View v) {requestpermission (Manifest.permission.CAMERA, CAMERA);
 }
  }); /** * photo, return the absolute path to the photo file * * Private string Takephotobypath (string filePath, int requestcode) {File File = new file
  (FilePath);
  Startactivityforresult (gettakephotointent (file), Requestcode);
 return File.getpath ();
  Private Intent gettakephotointent (file file) {if (file.exists ()) {file.delete ();
  try {file.createnewfile ();
  catch (IOException e) {e.printstacktrace ();
  Uri uri = uri.fromfile (file);
  Intent Intent = new Intent (mediastore.action_image_capture);
Intent.putextra (Mediastore.extra_output, URI);  return intent;
 public boolean isgranted (String permission) {return!ismarshmallow () | | | isgranted_ (permission); Private Boolean isgranted_ (String permission) {int checkselfpermission = Activitycompat.checkselfpermission (this, p
  Ermission);
 return checkselfpermission = = packagemanager.permission_granted; Private Boolean Ismarshmallow () {return Build.VERSION.SDK_INT >= build.version_codes.
 M //shouldshowrequestpermissionrationale is primarily used to give users an explanation of the application permissions that only the user has denied you in the last permission request.
 In other words, the user has rejected the once, you play an authorization box, you need to give the user an explanation, why to authorize, then use this method. private void Requestpermission (String permission, int requestcode) {if (!isgranted (permission)) {if (Activitycompat . Shouldshowrequestpermissionrationale (this, permission)) {} else {activitycompat.requestpermissions (this, new St
   Ring[]{permission}, Requestcode); } else {//directly performed the corresponding operation}} @Override public void Onrequestpermissionsresult (int requestcode, string[] Permissio NS, int[] Grantresults {if (RequestCode = = CAMERA) {if (grantresults[0] = packagemanager.permission_granted) {String Jpgpath = Getcachedir () + "tes
    T.jpg ";
   Takephotobypath (Jpgpath, 2);
   else {//Permission Denied Toast.maketext (mainactivity.this, "You do not authorize this permission, open authorization in Settings", Toast.length_short). Show ();
  } return;
 } super.onrequestpermissionsresult (Requestcode, permissions, grantresults);

 }

}

Summarize:

This article summarizes the Android 6.0 runtime permissions and how to fit the problem, but this is not our company's current final solution, from the above can see that the implementation is still quite troublesome, request permission and processing callback in different places code readability is relatively poor, Our Final Solution is a rxjava+rxpermission solution, and the next one will explain how to use Rxpermission to address the Android 6.0 rights adaptation issue.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.