Android Release 6.0 after the app to run the permission prompts for a friendly prompt, similar to the Apple system, such as the use of a page to call permissions, will pop up a prompt box, indicating whether you need to agree to this permission, if you agree to the app has the right to call, you can call the phone, do not agree not to call the electricity , you can only go to the settings check, before 6.0, the permission configuration is added in the Androidmanifest.xml file, for example:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "Package =" Com.example.ymx.dynamicpermissiondemo "> <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission> <uses-permission android:name= " Android.permission.CALL_PHONE "></uses-permission> </manifest>
Two permissions have been added to the configuration file, one is the network status and one is the call permission.
To set up a call in code:
Public classMainactivityextendsappcompatactivity {PrivateButton Mcallbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mcallbutton=(Button) Findviewbyid (R.id.call_button); Mcallbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent callintent=NewIntent (); Callintent.setaction (Intent.action_call); Callintent.setdata (Uri.parse ("Tel:" + "10086")); StartActivity (callintent); } }); }}
The phone can dial out normally.
If you do not have permission to register, you will be reported the relevant exception:
FATAL exception:main 3147 java.lang.SecurityException:Permission denial:starting Intent {Act=android.intent.action.call Dat=tel:xxxxxxx cmp=com.android.server.telecom/. Callactivity} from processrecord{3d45b392 3147:com.example.ymx.dynamicpermissiondemo/u0a59} (pid=3147, uid=10059 ) requires Android.permission.CALL_PHONE at android.os.Parcel.readException (Parcel.java:1546)
Now let's see how the Android6.0 version implements runtime permission reminders, first configured in the app's Gradle targetsdkversion=25
PackageCom.example.ymx.dynamicpermissiondemo;ImportAndroid. Manifest;Importandroid.content.Intent;ImportAndroid.content.pm.PackageManager;ImportAndroid.net.Uri;ImportAndroid.support.annotation.NonNull;ImportAndroid.support.v4.app.ActivityCompat;ImportAndroid.support.v4.content.ContextCompat;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {PrivateButton Callbutton; Private Static Final intPermission_request_code = 1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Callbutton=(Button) Findviewbyid (R.id.call_button); Callbutton.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {checkpermission (); } }); } //detecting whether a user agrees to a permission Private voidcheckpermission () {//determines whether the requested permission has passed, does not pass return False, by returning true, then prompts out and dials the phone if(Contextcompat.checkselfpermission ( This, Manifest.permission.CALL_PHONE)! =packagemanager.permission_granted) { //Request permission callback functionActivitycompat.requestpermissions ( This,NewString[]{manifest.permission.call_phone},permission_request_code); }Else{Toast.maketext ( This, "Permission has been applied!" ", Toast.length_short). Show (); Call (); } } //How to call Private voidCall () {Intent callintent=NewIntent (); Callintent.setaction (Intent.action_call); Callintent.setdata (Uri.parse ("tel://10086")); StartActivity (callintent); } //permission Request callback method@Override Public voidOnrequestpermissionsresult (intRequestcode, @NonNull string[] permissions, @NonNullint[] grantresults) { Super. Onrequestpermissionsresult (Requestcode, permissions, grantresults); Switch(requestcode) { CasePermission_request_code://permission to agree to apply if(grantresults.length>0&&grantresults[0]==packagemanager.permission_granted) {Toast.maketext ( This, "agreed to pass", Toast.length_short). Show (); Call (); //permission to deny a request}Else{Toast.maketext ( This, "Refuse to pass", Toast.length_short). Show (); } Break; default: Break; } }}
As can be seen, the definition is very simple, in the activity wrote three methods, respectively, call, permission check, permission to apply the callback function, The Check permission method mainly uses the Contextcompat.checkselfpermission method, inside receives two parameters, the first one is the activity instance, the second is the permission name that you want to apply, packagemanager.permission_granted This parameter means that the permission has passed, and contextcompat.checkselfpermission The results of the comparison, and then do the corresponding processing, if you have already passed the toast out, and then directly dial the phone??, if not passed, request a callback permission method,
Method is activitycompat.requestpermissions, inside receive three parameters, one is an activity instance, the second is multiple permission name array, the third is Requestcode, this requestcode is not very familiar, As in Startactivityforresult, distinguish between different permissions processing.
In the Onrequestpermissionsresult to determine whether the user passed a permission, using the int[] grantresults parameter, first determine its length, the angle mark is 0, that is, Packagemanager.permission_granted, the angle mark of 1 indicates failure, i.e. Packagemanager. Permission_denied, take a look at the effect of the operation ~ ~ ~
Finally, do not forget to register the relevant uses-permission in the Androidmanifest.xml, this is no matter which version is required to register ~ ~ ~????。
The above only introduces the 6.0 version of the basic usage of dynamic permission reminders, when you want to apply for multiple permissions, you have to think about how to modify these logic,
Run-time permission reminders after Android version 6.0