Android 6.0+ Run-time permissions

Source: Internet
Author: User

1. The authority is divided into two types: normal and dangerous.

2. Call the Demo

Import Android. Manifest;import Android.app.activity;import Android.content.intent;import Android.content.pm.PackageManager; Import Android.net.uri;import Android.os.bundle;import Android.provider.settings;import Android.support.v4.app.activitycompat;import Android.support.v4.content.contextcompat;import Android.text.textutils;import Android.view.view;import Android.widget.button;import Android.widget.EditText; Import Android.widget.toast;public class Mainactivity extends Activity {private static final int my_permissions_reques    T_call_phone = 1;    Private Button btn_dial;    Private EditText Et_number;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Locate the control Et_number = (EditText) Findviewbyid (R.id.et_number);        Btn_dial = (Button) Findviewbyid (r.id.btn_dial);   Set the monitor (click event) Btn_dial.setonclicklistener (new View.onclicklistener () {//Anonymous inner class for the button         When the button is clicked, the callback @Override public void OnClick (view view) {//checks whether the permission has been granted (ANDROID6. 0 Runtime permissions) if (Contextcompat.checkselfpermission (Mainactivity.this, manifest.permission . Call_phone)! = packagemanager.permission_granted) {//not authorized, apply for authorization if (Activitycompat                        . Shouldshowrequestpermissionrationale (Mainactivity.this, Manifest.permission.CALL_PHONE)) { return value://If the app has previously requested this permission and is rejected by the user, this method will return true.//if the user                        When you check the "Don't ask again" option in the dialog box when the permission is denied, this method returns false.//if the device policy prohibits the app from owning this permission, this method also returns false. The popup needs to explain why this permission is required, and again request authorization Toast.maketext (Mainactivity.this, "please authorize!                        ", Toast.length_long). Show (); Help jump to the app's settings screen and let the user manually authorize Intent Intent = new Intent (settings.action_application_details_settinGS);                        Uri uri = uri.fromparts ("Package", Getpackagename (), NULL);                        Intent.setdata (URI);                    StartActivity (Intent); }else{//Do not need to explain why this permission is required, direct request Authorization Activitycompat.requestpermissions (mainactivity . This, new String[]{manifest.permission.call_phone}, My_perm                    Issions_request_call_phone);                }}else {//has already been authorized to call Callphone ();    }            }        });        private void Callphone () {String number = Et_number.gettext (). toString ();            if (textutils.isempty (number)) {//Reminder user//Note: In this anonymous inner class, if this is the object of the View.onclicklistener class,            Therefore, you must use Mainactivity.this to specify the context environment. Toast.maketext (Mainactivity.this, "number cannot be empty!")        ", Toast.length_short). Show (); } else {//dialing: Activation systemDial-up component Intent Intent = new Intent (); Intent object: Action + data intent.setaction (Intent.action_call); Set the action Uri data = Uri.parse ("Tel:" + number);            Set data intent.setdata; StartActivity (Intent); Activate activity Component}}//callback for processing permission request @Override public void Onrequestpermissionsresult (int requestcode, stri                ng[] permissions, int[] grantresults) {switch (requestcode) {case My_permissions_request_call_phone: { if (grantresults.length > 0 && grantresults[0] = = Packagemanager.permissi                on_granted) {//authorization succeeded, continue to call Callphone ();                    } else {//authorization failed! Toast.maketext (This, "Authorization failed!                ", Toast.length_long). Show ();            } break; }        }    }}

  

Android 6.0+ Run-time permissions

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.