Program run is divided into two kinds of permissions, one is normal permissions, one is dangerous permissions. Normal permission can be used directly, only the dangerous permission needs to ask the user's consent.
The Dangerous permissions group and the permissions within the group are as follows:
Note: If a permission is granted within the same group of dangerous permissions, a set of permissions is granted.
1group:android.permission-Group. CONTACTS2 Permission:android.permission.WRITE_CONTACTS3 Permission:android.permission.GET_ACCOUNTS4 Permission:android.permission.READ_CONTACTS5 6group:android.permission-Group. PHONE7 Permission:android.permission.READ_CALL_LOG8 Permission:android.permission.READ_PHONE_STATE9 Permission:android.permission.CALL_PHONETen Permission:android.permission.WRITE_CALL_LOG One Permission:android.permission.USE_SIP A Permission:android.permission.PROCESS_OUTGOING_CALLS - Permission:com.android.voicemail.permission.ADD_VOICEMAIL - thegroup:android.permission-Group. CALENDAR - Permission:android.permission.READ_CALENDAR - Permission:android.permission.WRITE_CALENDAR - +group:android.permission-Group. CAMERA - Permission:android.permission.CAMERA + Agroup:android.permission-Group. Sensors at Permission:android.permission.BODY_SENSORS - -group:android.permission-Group. Location - Permission:android.permission.ACCESS_FINE_LOCATION - Permission:android.permission.ACCESS_COARSE_LOCATION - ingroup:android.permission-Group. STORAGE - Permission:android.permission.READ_EXTERNAL_STORAGE to Permission:android.permission.WRITE_EXTERNAL_STORAGE + -group:android.permission-Group. Microphone the Permission:android.permission.RECORD_AUDIO * $group:android.permission-group.smsPanax Notoginseng Permission:android.permission.READ_SMS - Permission:android.permission.RECEIVE_WAP_PUSH the Permission:android.permission.RECEIVE_MMS + Permission:android.permission.RECEIVE_SMS A Permission:android.permission.SEND_SMS thePermission:android.permission.READ_CELL_BROADCASTS
Request Permission:
For example, to get the call permission to call.
First, declare the required permissions in the Androidmanifest.xml file:
<uses-permission android:name="android.permission.CALL_PHONE"/>
Set a button in the layout file, and then write the logic for the request permission in Mainactivity.
The application is divided into three main parts:
1. Set the button click event, through the Checkselfpermission method to determine whether there is permission, if there is no permission, then call the Requestpermissions method to ask whether the user is authorized, if there is permission, call () directly to execute.
2. After the first step calls the Requestpermissions method, regardless of whether the user chooses to authorize or not, will callback to the Onrequestpermissionsresult () method, the authorization result is encapsulated in the Grantresults parameter, so the authorization result is judged , the call method is called if the authorization is granted, and the prompt is not authorized if you do not agree.
3.call method, using stealth intent method, the action for dialing the phone, and set the corresponding telephone number, perform intent action. To prevent the program from collapsing, these are executed in the try method.
The code is as follows:
1 Public classMainactivity extends Appcompatactivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 super.oncreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8 /*Set the button click event to determine if there is a permission by using the Checkselfpermission method9 * If there is no permission, call the Requestpermissions method to ask the user whether to authorizeTen * Call () is called directly if there is permission to execute*/ OneButton MakeCall =(Button) Findviewbyid (r.id.make_call); AMakecall.setonclicklistener (NewView.onclicklistener () { - @Override - Public voidOnClick (view view) { the /*the method passes two parameters, the first is the context, and the second is the permission name to get. - Compare the return value to packagemanager.permission_granted*/ - if(Contextcompat.checkselfpermission (mainactivity. This, Manifest.permission.CALL_PHONE) -!=packagemanager.permission_granted) { + /*To obtain authorization, call the Requestpermissions method to request authorization from the user, - Pass in three parameters: context, the obtained permission name string, and the request code, the unique value can be. */ +Activitycompat.requestpermissions (mainactivity. This,NewString[]{manifest.permission.call_phone},1); A}Else { at Call (); - } - } - }); - } - in /*Call method specific, using stealth intent method, the action for dialing the phone, and set the corresponding telephone number, perform intent action. - * In order to prevent the program from crashing, these are put in the try method to execute. */ to Private voidCall () { + Try { -Intent Intent =NewIntent (intent.action_call); theIntent.setdata (Uri.parse ("tel:10086")); * startactivity (intent); $}Catch(SecurityException e) {Panax Notoginseng e.printstacktrace (); - } the } + A @Override the Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions, + int[] grantresults) { - Switch(requestcode) { $ Case 1: $ if(Grantresults.length >0&& grantresults[0] ==packagemanager.permission_granted) { - Call (); -}Else { theToast.maketext ( This,"You denied the permission", Toast.length_long). Show (); - }Wuyi Break; the default: - } Wu } -}
Android Content provider-run permissions