Android6.0-run-time Permission Handling

Source: Internet
Author: User
<span id="Label3"></p><p><p>Why do I need to have run-time permissions?</p></p><p><p>As we all know before Android6.0, permissions are only asked once during the app installation process and presented to the user as a list, and if you click Cancel (that is, do not endorse the App's requested permission), the App's installation will be Canceled. For the user to install the application needs, generally will click agree, and the application may be in the background to do some illegal operations. (at the same time, for this reason, applications may appear to apply for a lot of permissions, maybe later, hhhhh)</p></p><p><p>It is the realization of this problem that, after the Android6.0 version, the runtime permissions feature is Introduced. That is, users do not need to be licensed at the time of application installation, but they can authorize a certain permission during the Application's Use.</p></p><p><p>Of course not all permissions do not require manual dynamic application, Android will be divided into normal permissions and high-risk permissions. For normal permissions, The system automatically completes the authorization, we only need to handle the authorization of the high-risk Permission. Below is the list of dangerous permissions for Android.</p></p> <table border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td valign="top" width="217"><p align="center"><strong>Permission Group</strong></p></td> <td valign="top" width="274"><p align="center"><strong>Permissions</strong></p></td> </tr> <tr> <td valign="top" width="217"><p align="center">CALENDAR</p></td> <td valign="top" width="274"> <ul> <li>Read_calendar</li> </ul> <ul> <li>Write_calendar</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">CAMERA</p></td> <td valign="top" width="274"> <ul> <li>CAMERA</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">CONTACTS</p></td> <td valign="top" width="274"> <ul> <li>Read_contacts</li> <li>Write_contacts</li> <li>Get_accounts</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">Location</p></td> <td valign="top" width="274"> <ul> <li>Access_fine_location</li> <li>Access_coarse_location</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">Microphone</p></td> <td valign="top" width="274"> <ul> <li>Record_audio</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">PHONE</p></td> <td valign="top" width="274"> <ul> <li>Read_phone_state</li> <li>Call_phone</li> <li>Read_call_log</li> <li>Write_call_log</li> <li>Add_voicemail</li> <li>Use_sip</li> <li>Process_outgoing_calls</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">Sensors</p></td> <td valign="top" width="274"> <ul> <li>Body_sensors</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">Sms</p></td> <td valign="top" width="274"> <ul> <li>Send_sms</li> <li>Receive_sms</li> <li>Read_sms</li> <li>Receive_wap_push</li> <li>Receive_mms</li> </ul></td> </tr> <tr> <td valign="top" width="217"><p align="center">STORAGE</p></td> <td valign="top" width="274"> <ul> <li>Read_external_storage</li> <li>Write_external_storage</li> </ul></td> </tr> </tbody> </table><p><p></p></p><p><p>Note: each dangerous permission in the table belongs to a permission group, we use the permission name when we run the permission processing, but when the user agrees to authorize, all other permissions for the permission group corresponding to that permission are authorized Simultaneously.</p></p><p><p>That is, once Read_calendar is authorized, the application also has Write_calendar Permissions.</p></p><p><p>2. Run-time Permission Handling</p></p><p><p>For applications running on the API23 system, if its targetapi is greater than or equal to 23, the runtime permission needs to be applied, or an exception is thrown, causing the program to Crash.</p></p><p><p>It is also easy to add runtime permissions to existing code, simply by nesting the code for permission requests where you need to apply for dangerous permissions, and here is a typical code for run-time permission Requests.</p></p><p><p>  </p></p><pre>Package Com.pignet.runtimepermissiondemo;import Android. Manifest;import Android.content.dialoginterface;import Android.content.intent;import Android.content.pm.packagemanager;import Android.net.uri;import Android.support.annotation.nonnull;import Android.support.v7.app.alertdialog;import Android.support.v7.app.appcompatactivity;import android.os.Bundle; Import Android.view.view;import Android.widget.button;import Android.widget.toast;public class MainActivity extends Appcompatactivity {private final static String call_permission=manifest.permission.call_phone; Private final static int request_code_ask_permissions = 1; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Button btncall= (button) Findviewbyid (r.id.btn_call); Btncall.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (View V) { CallwrappER (); } }); /** * If no permissions are applied, otherwise call */private void Callwrapper () {if (android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES. M) {int hascallphonepermission = checkselfpermission (call_permission); Whether you already have the specified permission if (hascallphonepermission!=packagemanager.permission_granted) {/** * Check whether you need to eject Request Permission dialog box * Shouldshowrequestpermissionrationale (permission) default return False * when the user refuses to give After the permission, the return value is true, the idea here is that when the user denies a permission * need to pop up a prompt box, tell the user the purpose of this permission to better apply to the permissions */if (sho Uldshowrequestpermissionrationale (CALL_PERMISSION)) {showmessageokcancel ("you need permission to allow calls", new Dialoginte Rface. Onclicklistener () {@Override public void OnClick (dialoginterface dialog, in T Which) {requestpermissions (new String[]{call_permission},request_code_ask_permissions); } }); Return } requestpermissions (new string[]{call_permission},request_code_ask_permissions); Return } Call (); }else Call (); private void Showmessageokcancel (String message, dialoginterface.onclicklistener oklistener) {new alertdialog . Builder (mainactivity.this). setmessage (message). Setpositivebutton ("ok", oklistener) . Setnegativebutton ("cancel", null). Create (). show (); }/** * action to complete */private void call () {try{Intent Intent = new Intent (intent.action_call ); Intent.setdata (uri.parse ("tel:110")); StartActivity (intent); }catch (securityexception E) {e.printstacktrace (); }}/** * * @param requestcode permission request code here corresponds to the previous 1 * @param permissioNS requires permission to request * @param grantresults request result -1:denied 0:granted */@Override public void Onrequestpermissionsre Sult (int requestcode, @NonNull string[] permissions, @NonNull int[] grantresults) {switch (requestcode) { Case 1:if (grantresults.length>0&&grantresults[0]==packagemanager.permission_granted) Call (); Else//when the user chooses to deny authorization for the first time, when requesting permission again, then the authorization dialog will be prompted with a "no longer alert", if you choose to deny Authorization//and tick no longer authorized, you will not go to the next read Request authorization, but directly in the callback to indicate that the user has refused authorization//so in this direction, prompting the user to "settings" in the manual authorization, or can send a broadcast to open the settings interface. Toast.maketext (this, "you have not granted this permission, please open authorization in settings", toast.length_short). show (); Break Default:super.onRequestPermissionsResult (requestcode, permissions, grantresults); Break } }}</pre><p><p>Note: as mentioned earlier, when a permission is requested, all permissions in this permission group are Obtained.</p></p><p><p>GitHub Address: Https://github.com/pignet/RuntimePermissionDemo</p></p><p><p>Android6.0-run-time Permission Handling</p></p></span>

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.