<span id="Label3"></p>Introduction to Run-time Permissions<p><p>Android 6.0 on the basis of our original Androidmanifest.xml claims authority, but also new runtime permissions dynamic detection, The following permissions need to be judged at runtime:</p></p><pre><pre>Body Sensor Calendar camera Address Book location microphone phone SMS storage space</pre></pre>Run-time Permission Handling<p><p>Android6.0 system default is targetsdkversion less than 23 of the app has been granted all the permissions requested, so if your previous app set the Targetsdkversion less than 23, at runtime will not crash, but this is only a temporary emergency policy, the user can still set the To cancel the granted permission in The.</p></p> <ul> <ul> <li>Declaring the target SDK version<br>We need to declare targetsdkversion as 23 in Build.gradle.<p></p><pre>Android {compilesdkversion buildtoolsversion "23.0.1" defaultconfig { applicationid "com.yourcomany.app Minsdkversion targetsdkversion versioncode 1 versionname "1.0"} buildtypes {release {minifyenabled F Alse proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '}}}</pre></li> </ul> </ul> <ul> <li><li>Check and Request permissions<br>We need to use the right place, every time we check whether the app has permissions, such as we have a download function, we need to write the SD card permissions, we write before writing to check whether there is write_external_storage permission, no request permission<p></p><pre> If (contextcompat.checkselfpermission (this, Manifest.permission.WRITE_EXTERNAL_STORAGE)! = Packagemanager.permission_granted) { //apply write_external_storage permission activitycompat.requestpermissions ( this, new string[]{manifest.permission.write_external_storage}, write_external_storage_request_code); }</pre></li></li> <li>When the permission is requested, the system will eject the request permission dialog <ul> <ul> <li>After the user chooses to allow or needs, the Onrequestpermissionsresult method is recalled, similar to the Onactivityresult<pre> @Override public void Onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) { Super.onrequestpermissionsresult (requestcode, permissions, grantresults); Donext (requestcode,grantresults); }</pre></li> <li>We then need to follow up with Requestcode and Grantresults (authorization results).<pre>private void Donext (int requestcode, int[] grantresults) { if (requestcode = = Write_external_storage_request_code) { if (grantresults[0] = = Packagemanager.permission_granted) { //PERMISSION granted } else { // Permission Denied }}} </pre></li> </ul> </ul>Special handling of Run-time permissions in fragment <ul> <ul> <li>Apply permissions in fragment, do not use activitycompat.requestpermissions, use fragment Requestpermissions method directly, Otherwise you will be recalled to the activity Onrequestpermissionsresult</li> <li>If you nest fragment in fragment, Requestpermissions method is used in sub-fragment, Onrequestpermissionsresult does not callback back, we recommend using Getparentfragment (). requestpermissions method, This method will call back to the Onrequestpermissionsresult in the parent fragment, and add the following code to pass the callback to the child fragment<pre> @Override public void Onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) { Super.onrequestpermissionsresult (requestcode, permissions, grantresults); list<fragment> fragments = Getchildfragmentmanager (). getfragments (); If (fragments! = Null) {for (Fragment fragment:fragments) { If (Fragment! = Null) { fragment.onrequestpermi Ssionsresult (requestcode,permissions,grantresults);}}} </pre></li> </ul> </ul>Related Open source projects<p><p>Permissionsdispatcher<br>Dynamically generated classes handle Run-time permissions using annotations, and nested fragment are not currently Supported.</p></p><p><p>Rxpermissions<br>rxjava-based Runtime Permission Detection framework</p></p><p><p>Grant<br>simplifies the handling of run-time permissions and is more flexible</p></p><p><p>Android-runtimepermissions<br>Google's Official Example</p></p>Appendix<p><p>The following permissions only need to be declared in Androidmanifest.xml to use</p></p><pre>Android.permission.ACCESS_LOCATION_EXTRA_COMMANDSandroid.permission.ACCESS_NETWORK_ STATEandroid.permission.ACCESS_NOTIFICATION_POLICYandroid.permission.ACCESS_WIFI_ STATEandroid.permission.ACCESS_WIMAX_STATEandroid.permission.BLUETOOTHandroid.permission.BLUETOOTH_ ADMINandroid.permission.BROADCAST_STICKYandroid.permission.CHANGE_NETWORK_STATEandroid.permission.CHANGE_WIFI_ MULTICAST_STATEandroid.permission.CHANGE_WIFI_STATEandroid.permission.CHANGE_WIMAX_ STATEandroid.permission.DISABLE_KEYGUARDandroid.permission.EXPAND_STATUS_ BARandroid.permission.FLASHLIGHTandroid.permission.GET_ACCOUNTSandroid.permission.GET_PACKAGE_ SIZEandroid.permission.INTERNETandroid.permission.KILL_BACKGROUND_PROCESSESandroid.permission.MODIFY_AUDIO_ SETTINGSandroid.permission.NFCandroid.permission.READ_SYNC_SETTINGSandroid.permission.READ_SYNC_ STATSandroid.permission.RECEIVE_BOOT_COMPLETEDandroid.permission.REORDER_TASKSandroid.permission.REQUEST_ INSTALL_PACKAGESandroid.permission.SET_TIME_ZONEandroid.permission.SET_WALLPAPERandroid.permission.SET_WALLPAPER_HINTSandroid.permission.SUBSCRIBED_FEEDS_ READandroid.permission.TRANSMIT_IRandroid.permission.USE_ FINGERPRINTandroid.permission.VIBRATEandroid.permission.WAKE_LOCKandroid.permission.WRITE_SYNC_ SETTINGScom.android.alarm.permission.SET_ALARMcom.android.launcher.permission.INSTALL_ SHORTCUTcom.android.launcher.permission.UNINSTALL_SHORTCUT</pre></li> </ul><p><p> Android 6.0 Runtime permission handling </p> </p></span>
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