Android App permissions Brief introductionAn Android app does not have any permissions by default, which means that, by default, an app has no right to do something that might cause bad effects. These bad effects may be for other applications, operating systems, or users. If the app requires some extra power, it needs to statically declare the appropriate permissions in Androidmanifest.xml. If the app does not declare permissions in manifest, but uses the appropriate functionality, an exception will be thrown when the function is called. For example, to send a request, but forget to add Internet access, the program will throw an exception when sending this request, This exception is generally not catch, so the program crashes directly:caused By:java.lang.SecurityException:Permission denied (missing INTERNET Permission?)InAndroid 6.0 (API)Before publishing, all permissions are displayed to the user when the app is installed, and the user chooses to install to accept them all, and then the authorization for those permissions cannot be revoked. Starting with Android 6.0, some of the more dangerous permissions require explicit bullets when the program is running, requesting authorization from the user. As to when to play this box, it is up to the application to decide. For other permissions, it is not very dangerous, so still keep the original practice, Authorized when the user installs the application. It is also important to note that in settings, the user can selectively authorize or close the application for dangerous permissions.permission level of protectionThe protection level of the permission is set by the ProtectionLevel property, which consists of 4 types:Normal,dangerous,signature,signatureorsystem.Refer to: Http://developer.android.com/guide/topics/manifest/permission-element.html signature related comparisons are not commonly used, the remaining two areNormalAnd dangerous.Guides in the face of these two types are discussed: official website guides:https://developer.android.com/guide/topics/security/permissions.html# Normal-dangerous Summary is: All the permissions are still in manifest static declaration, the normal permissions are automatically authorized at the time of installation, and dangerous permissions need to apply explicitly request user authorization. Of course for Android 6.0 or less, or previously developed legacy applications, dangerous permissions are also authorized at the time of installation, see the next section of the discussion. Dangerous Permissions:
Table 1. Dangerous permissions and permission groups.
| Permission Group |
Permissions |
CALENDAR |
|
CAMERA |
|
CONTACTS |
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
|
LOCATION |
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
|
MICROPHONE |
|
PHONE |
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
|
SENSORS |
|
SMS |
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
|
STORAGE |
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
|
To view all dangerous permissions, you can also use the command:adb shell PM list permissions-g-Ddifferent handling of mobile version and program versionHere is a reference to the original text inside a guildes:
- If the device is running Android 6.0 (API level) or higher, and the app's is
targetSdkVersion or higher, the app R Equests permissions from the user at Run-time. The user can revoke the permissions at any time, so the app needs to check whether it has the permissions every time it ru Ns. For more information about requesting permissions in your apps, see the Working with System Permissions Training Guide.
- If the device is running Android 5.1 (API level) or lower, or the app's is
targetSdkVersion A or lower, the system a SKS the user to grant the permissions when the user installs the app. If you add a new permission to an updated version of the app, the system asks the user to grant this permission when the U SER updates the app. Once the user installs the app, the only to they can revoke the permission is by uninstalling the app.
Here the head should pay attention to the use of and and OR, it shows that only meet targetsdkversion and the actual use of the device version is 23 and above, the new dynamic permission mechanism will be adopted. In other cases, as before, all permissions are granted when the app is installed and upgraded.can be summed up as:1. All permissions are declared in manifest. 2. if (1) Your app's targetsdkversion is 23 and above, and (2) the app is running on Android 6.0 and above, dangerous permissions must be requested dynamically. When permissions are denied, The app should still be able to use it, except that some of the rights-related features are not available. 3. The two conditions in the previous article (1) (2) are not met at the same time, that is, all permissions are requested at the time of installation, and are not installed if the user does not accept it.pay particular attention to this situation: the old application of the new system.If Targetsdkversion is less than 23, it is considered an app developed before Android 6.0 is released and is not yet compatible with 6.0.this application, even on a machine installed on Android 6.0, is granted the permission logic using the original installation, all permissions are granted when the app is installed. After installing targetsdkversion less than 23 on Android 6.0 devices, you can see in the app's settings that all dangerous permission states are open. So do not worry about the old app on Android 6.0 will be a variety of random collapse.However, users can still disable permissions in system settings:When you click on the authorization switch on the simulator, the following prompt pops up: If the user insists on canceling the authorization, although the application does not crash directly, but the function becomes silently stateless, the return value may become null or 0, which can cause unpredictable behavior or crash.Why to upgrade targetsdkversion in timeThis is because each version of the API has the potential to generate new permissions, and these new permissions are automatically obtained for targetsdkversion applications that are lower than the API. So targetsdkversion better be able to write to the latest in time, This prevents the app from automatically acquiring the new permissions that are added to the API. Conclusion: Permissions that do not exist for Targetsdkversion are automatically acquired. See also: API guides:https://developer.android.com/guide/topics/security/permissions.html where "Automatic permission Adjustments "that paragraph.Permission GroupAll permissions have their own permission group. The System bullets request a certain permission when it is also only described its category, when the user agrees, the system will give it the bar permission. (only this one). However, if the app already has another permission under the group, the system will automatically grant the permission (that is, the callback that requests the permission directly), which does not interact with the user.implementation of dynamic permission requestOriginal: https://developer.android.com/training/permissions/requesting.html Because of the permission dynamic check related API is Android 6.0 to join, So minsdkversion is not 23 o'clock, it is recommended to use supportlibrary to achieve, the advantage is: The program does not have to add if to determine the current device version.1. Check permission statusIf you perform an operation that requires a dangerous permission, you must check to see if you have this permission every time you perform the operation, because the user can arbitrarily change the authorization situation in the app settings, so it must be checked for permission each time before use. Methods for checking permissions: Contextcompat.checkselfpermission () Two parameters are context and permission name. The return value is:PERMISSION_GRANTEDIf you have the permission, orPERMISSION_DENIEDIf not. Like what:
if (packagemanager.permission_granted = = Contextcompat.checkselfpermission (mainactivity. This, Manifest.permission.READ_CONTACTS)) { //havepermission, do operation directly Contactsutils.readphonecontacts (this); LOG.I (Debug_tag, "User has the permission already!" Else/ don't have permission
2. Dynamic Request PermissionsIf the result of the above permission check is denied, then you need to explicitly request this permission from the user. Android provides several methods to dynamically request permissions, and calling these methods will show a standard dialog, which dialog is currently not customizable.2.1 Sometimes it may be necessary to explain why this permission is requiredSometimes you may need to explain to the user the purpose of the permission. Note Not every permission needs to be explained, the obvious one can not explain, too many explanations will degrade the user experience. One way is, when the user denies this permission, but also uses this function, then probably the user does not quite understand why the app needs this permission, this time can explain to the user first. To find out what this user might need to explain, Android provides a tool-class approach: Shouldshowrequestpermissionrationale () This method returns True if the app has previously requested this permission and was rejected by the user. If the user previously denied permission, tick the "Don't ask again" option in the dialog box, Then this method returns False. This method also returns false if the device policy prohibits the app from owning this permission. Note that the specific explanation of the reason for this dialog needs to be implemented by itself, not provided by the system.2.2 Request PermissionPermission is requested by Requestpermissions () passing in an activity, an array of permission names, and an integer request code. This method is asynchronous, and it returns immediately when the user and dialog have finished interacting , the system invokes a callback method that returns the user's selection result and the corresponding request code.
if (packagemanager.permission_granted = = Contextcompat.checkselfpermission (mainactivity.This, Manifest.permission.READ_CONTACTS)) {//have permission, do operation directly contactsutils.readphonecontacts (This); LOG.I (Debug_tag, "User has the permission already!");}Else{//Do not have permission log.i (Debug_tag, "user does not has this permission!");//Should we show an explanation?if (Activitycompat.shouldshowrequestpermissionrationale (mainactivity.This, Manifest.permission.READ_CONTACTS)) {//Show an explanation to the user *asynchronously*--don ' t block//This thread waiting for the user ' s response! After the user//sees the explanation, try again to request the permission. LOG.I (Debug_tag, "we should explain why we need this permission!"); }else {// No explanation needed, we can request the permission. LOG.I (Debug_tag, "==request the permission==" Thisnew string[]{ Manifest.permission.READ_CONTACTS}, My_permissions_request_read_contacts); // My_permissions_request_read_contacts was an // app-defined int constant. The callback method gets the // result of the Request. }}This dialog box is system and cannot be customized. The "Don't ask again" option in the Request Permission dialog box appears only if the state before the permission is denied. Previously never authorized (that is, the first frame), or the previous state is granted ( Of course, this is usually not the case to ask the box, the pop-up box is not with the option to no longer ask.2.3 Response to request permission processingWhen a user responds to a dialog that requests permission, the system callsonRequestPermissionsResult()method to return the user's response. The request code in this callback is the parameter passed in when calling Requestpermissions () and is an integer value that the app customizes. If the request is canceled, the returned array will be empty. Code:
@OverridePublicvoid Onrequestpermissionsresult (IntRequestcode, String permissions[],Int[] grantresults) {Switch(Requestcode) {CaseMy_permissions_request_read_contacts: {//If request is cancelled, the result of arrays is empty.if (grantresults.length > 0 && grantresults[0] = =packagemanager.permission_granted) {//Permission was granted, yay! Do the//contacts-related task need to do. Contactsutils.readphonecontacts (this); LOG.I (Debug_tag, "user granted the permission!" ); } Else { // permission denied, boo! Disable the // functionality that depends on this permission. LOG.I (Debug_tag, "user denied the permission!" ); } return; //Other ' case ' lines-check for the other // permissions This app might request }} System Auto-callback situation: In some cases, call 1. Automatic Authorization: If the user has allowed a permission in the permission group, the next time the Requestpermissions () method is called to request the B permission in the same group, the system will call directlyonRequestPermissionsResult()callback method, and returns the result of the permission_granted. 2. Auto-Reject: If the user chooses not to ask this permission again, then the app calls the Requestpermissions () method again to request the same permission. The system calls the Onrequestpermissionsresult () callback directly and returns the Permission_denied. Demo Address: Https://github.com/mengdd/AndroidMRuntimePermissionSampleBest practicesOriginal: Https://developer.android.com/training/permissions/best-practices.htmlBest Practices Summary: 1. Start other apps with intent. 2. Use only the permissions that are really needed. 3. Do not request multiple permissions at once to annoy the user, and some permissions can wait until the time to use the request. 4. Explain to the user why this permission is required. 5. Starting with Android 6.0, each permission, You need to test the switch in both situations to make the app work, not crash. And the associated permissions may need to test different combinations.adb commandThe command line can be used to manage permissions: Use the ADB tool to manage permssions from the command lines:
References:API guides:https://developer.android.com/guide/topics/security/permissions.htmltraining:https:// Developer.android.com/training/permissions/index.htmlruntime permissions:https://source.android.com/devices/ Tech/config/runtime_perms.htmlpermission element:http://developer.android.com/guide/topics/manifest/ permission-element.html Design Patterns-permissions:https://www.google.com/design/spec/patterns/permissions.html Blog post: http://jijiaxin89.com/2015/08/30/Android-s-Runtime-Permission/third-party library: permissionsdispatcher:https:// github.com/hotchemi/permissionsdispatcherrxpermissions:https://github.com/tbruyelle/rxpermissionsgrant:https:/ /github.com/anthonycr/grant This article address: http://www.cnblogs.com/mengdd/p/4892856.html turn from: http://www.cnblogs.com/mengdd/p /4892856.html
Android M Permission Learning Notes