Android 6.0 permissions application details and authority data collation _android

Source: Internet
Author: User
Tags call back

At the beginning of Android 6.0, some of the privileges required us to apply dynamically, that is, when we open the app, the system will not take the initiative like you need to apply for apps, and require customers to apply actively when using the app.

First, the application of the right of two steps:

1. Permission application:

/**
 * @param permissions Required Permission
 * @param requestcode Request Callback Code */
 public
 static void Requestpermissions (Final @NonNull activity activity,final @NonNullString [] permissions, final int requestcode)

2, the application callback processing:

 /**

 * @param requestcode Request Callback Code

 * @param permissions Request Permission

 * @param grantresults return request status packagemanager# Permission_granted (Grant) packagemanager#permission_denied (deny)/public

 void Onrequestpermissionsresult ( int Requestcode, @NonNull string[] permissions, @NonNull int[] grantresults)

Ii. Special handling of runtime permissions in fragment

Apply permission in fragment, do not use activitycompat.requestpermissions, direct use of fragment Requestpermissions method, Otherwise it will be recalled to the activity's onrequestpermissionsresult if the fragment is nested in the fragment, the Requestpermissions method is used in the child fragment, Onrequestpermissionsresult will not callback back, recommend the use of Getparentfragment (). Requestpermissions method,

This method will call back to the Onrequestpermissionsresult in the parent fragment and add the following code to pass the callback through to the child fragment

 @Override public

 void  onrequestpermissionsresult (int requestcode, string[] permissions, int[]  Grantresults) {

   super.onrequestpermissionsresult (requestcode,  permissions, grantresults);

   List  fragments = Getchildfragmentmanager (). getfragments ();

   if (fragments!= null) {for

   (Fragment  fragment:fragments) {

      if (Fragment!=  null) {

        Fragment.onrequestpermissionsresult (Requestcode,permissions,grantresults);}}}


Simple encapsulation of permission application

 public class Requestpermissions {public static intrequest_external_storage = 1; public static String[]permissions_storage = {Manifest.permission.READ_EXTERNAL_STORAGE, manifest.permis Sion.

   Write_external_storage}; /** * Read-write permission request callback True permissions false No permissions * * @param mcontext/public static voidwriteexternalstorage (final conte XT Mcontext, Permissioncallback mcallback) {//Check that you have the appropriate permission int permission =activitycompat.checkselfpermission (m

   Context,manifest.permission.write_external_storage);  No permission to initiate an application if (permission!=packagemanager.permission_granted) {//6.0 and the above version requires permission to request if (Build.VERSION.SDK_INT >= Build.version_codes.

      M) Activitycompat.requestpermissions ((activity) mcontext,permissions_storage,request_external_storage);

      else {Mcallback.setonpermissionlistener (false);

   } else {Mcallback.setonpermissionlistener (true);

  }/** * Read-write permission request callback true has permissions false no permissions * * @param mcontext/public static voidwriteexternalstorage (final Mcontext, Permissioncallback mcallb ACK, Intrequestcode) {//Check that you have the appropriate permission int permission =activitycompat.checkselfpermission (mcontext,manifest.permissi On.

   Write_external_storage); No permission to initiate an application if (permission!=packagemanager.permission_granted) {//6.0 and the above version requires permission to request if (Build.VERSION.SDK_ INT >= build.version_codes.

      M) Activitycompat.requestpermissions ((activity) mcontext,permissions_storage,requestcode);

      else {Mcallback.setonpermissionlistener (false);

   } else {Mcallback.setonpermissionlistener (true); /** * Permission Request callback, you need to call the method true in the activity you call Onrequestpermissionsresult Request permission succeeded false Request permission failed * * @param requestcode * @param permissions * @param grantresults/public static void Onrequestpermissionsresult (int requestcode, @NonNull string[] permissions, @NonNull int[] grantresults, Permissioncallback mcallback) {if(Requestcode ==request_external_storage)  {//Determine if permission is applied for success if (Grantresults.length > 0 && grantresults[0] ==packagemanager.permission_granted)

      {Mcallback.setonpermissionlistener (true);

      else {Mcallback.setonpermissionlistener (false);

   } return; }/** * 6.0 The following requests Open Rights Management * * @param mcontext/public static void Openpre (final context Mcontext) {Remi Nddialogutil.showreminddialog (Mcontext, "without the right of the moment, will not be the way to upgrade ~, whether to open settings?")

        ", new Reminddialogutil.dialogcallback () {@Override public void Clickyes () {//Jump to Settings page, open permissions manually

        Intent intent= new Intent ();

        Intent.setaction (settings.action_application_settings);

        Mcontext.startactivity (Intent);

      Reminddialogutil.hidereminddialog ();

      @Override public Voidclickcancel () {reminddialogutil.hidereminddialog ();

   }

   }); } public Interfacepermissioncallback {void SetonpermissIonlistener (Boolean bo);

 }

 }

Appendix

The following permissions need to be judged at run time:

Body Sensors

Calendar

Camera

Contacts

Location

Microphone

Phone

Sms

Storage space

The following permissions only need to be declared in Androidmanifest.xml to use

Android.permission.ACCESS_LOCATION_EXTRA_COMMANDS

Android.permission.ACCESS_NETWORK_STATE

Android.permission.ACCESS_NOTIFICATION_POLICY

Android.permission.ACCESS_WIFI_STATE

Android.permission.ACCESS_WIMAX_STATE

Android.permission.BLUETOOTH

Android.permission.BLUETOOTH_ADMIN

Android.permission.BROADCAST_STICKY

Android.permission.CHANGE_NETWORK_STATE

Android.permission.CHANGE_WIFI_MULTICAST_STATE

Android.permission.CHANGE_WIFI_STATE

Android.permission.CHANGE_WIMAX_STATE

Android.permission.DISABLE_KEYGUARD

Android.permission.EXPAND_STATUS_BAR

Android.permission.FLASHLIGHT

Android.permission.GET_ACCOUNTS

Android.permission.GET_PACKAGE_SIZE

Android.permission.INTERNET

Android.permission.KILL_BACKGROUND_PROCESSES

Android.permission.MODIFY_AUDIO_SETTINGS

Android.permission.NFC

Android.permission.READ_SYNC_SETTINGS

Android.permission.READ_SYNC_STATS

Android.permission.RECEIVE_BOOT_COMPLETED

Android.permission.REORDER_TASKS

Android.permission.REQUEST_INSTALL_PACKAGES

Android.permission.SET_TIME_ZONE

Android.permission.SET_WALLPAPER

Android.permission.SET_WALLPAPER_HINTS

Android.permission.SUBSCRIBED_FEEDS_READ

Android.permission.TRANSMIT_IR

Android.permission.USE_FINGERPRINT

Android.permission.VIBRATE

Android.permission.WAKE_LOCK

Android.permission.WRITE_SYNC_SETTINGS

Com.android.alarm.permission.SET_ALARM

Com.android.launcher.permission.INSTALL_SHORTCUT

Com.android.launcher.permission.UNINSTALL_SHORTCUT

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.