Original: Android permission dynamic application, Authorization
Android permission new features deep learning
This article describes the Android permission system and describes the new features of the Android 6.0 permission, including dynamic application and authorization of permissions.
Permission System
Android developers are aware that we have developed an application without any permissions, we have no way to network, there is no way to external space storage (internal space is possible), unless we apply for the appropriate permissions (permission).
For example, we add the following statement to the manifest file, and we can connect to the network in our app.
<uses-permission android:name="android.permission.INTERNET" />
So why is Android so designed? The author believes that the biggest advantage of such a design is that because the phone stores a large number of users of hidden information, for Android users, each time the app is installed, users will be prompted to what the app has permissions, so that the user has a general understanding of the app, the app may do what to do, Will steal my privacy and so on (of course, ordinary Android users are not aware of this).
Android divides all permissions into two, one is normal and the other is dangerous. As for the two, developer is so introduced,
Normal permissions cover areas where your app needs to access data or resources outside the app's sandbox, but where there ' s very little risk to the user's privacy or the operation of other apps. For example, permission-set the time zone is a normal permission. If An app is declares that it needs a normal permission, the system automatically grants the permission to the app.
In general, the normal type of permissions will expose users with little risk, so Android will give these permissions by default.
Dangerous permissions cover areas where the app wants data or resources that involve the user ' s private information, or CO Uld potentially affect the user ' s stored data or the operation of other apps. For example, the ability to read the user's contacts is a dangerous permission. If An app is declares that it needs a dangerous permission, the user have to explicitly grant the permission to the app.
And what is the dangerous authority? This type of permission can expose the user's privacy at a great risk, for example, by reading the user's address book.
With regard to the permissions of the dangerous class, developer gives the
Grant of permission
The above describes the danger permission is to be used by the user's permission,
Before 6.0
Prior to Android 6.0, these permissions were given to the user when they installed the app, and the user could choose to give that permission and then install the app, but the app wouldn't install if the user didn't give it, which is called all or nothing. Of course, this is the nature of the native system, and as far as I know, the system of Xiaomi and Meizu customization has been able to dynamically grant permissions.
After 6.0
Android 6.0 on this feature has been modified, after all, the mechanism of nothing is a big disadvantage, many users in order to use the app, they agreed to all permissions, and can not control it, so that the role of authority does not appear. So Android 6.0 allows permission to be dynamically granted.
Developer's description of it is as follows,
If the device is running Android 6.0 (API level) or higher, and the app's targetsdkversion is at or higher, the app req Uests 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
If the Android version of your device is above 6.0, and the app's target SDK version is above 23, users can dynamically assign their permissions. In other cases, the user cannot dynamically give permission, or all or nothing. Either give it all or do not install it.
So how do you use this new feature?
Check for Permissions
Develper's explanation is as follows,
If your app needs a dangerous permission, you must check whether it has that permission every time your perform an operat Ion that requires that permission. The user is always free to revoke the permission, so even if the app used the camera yesterday, it can ' t assume it still h As that permission today.
If our application includes dangerous permissions, we should check whether the permission is granted each time we perform the corresponding operation. Because the user can cancel a certain permission at any time, we should always check whether the user has given this permission. Otherwise, it will crash.
The function uses the following,
// Assume thisActivity is the current activityint permissionCheck = ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.WRITE_CALENDAR);
If the user gives the permission, the method returns packagemanager.permission_granted, and then we can proceed with our operation, otherwise it will return permission_denied. If we return to permission_denied, we will not be able to do our work directly, because we do not have permission. So what should we do? At this point, we should explicitly apply permissions.
So how does the application permission appear? This will use the following function,
Requestpermissions
When we find that our app is not granted a certain permission, we can display the application.
An example is as follows,
ActivityCompat.requestPermissions(thisnewString[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
Where My_permissions_request_read_contacts is the set Requestcode to distinguish which permission is requested, the function pops up a dialog box asking the user if the permission is authorized,
Users can choose to authorize or cancel, then how do we know the user's choice, which requires us to overload the acitivity of an interface,
@Override Public void 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 you need.System. out. println ("User granted permission"); }Else{System. out. println ("User didn ' t grante permission");//Permission denied, boo! Disable the //functionality that depends on the this permission.}return; }//Other ' case ' lines to check for other //Permissions This app might request} }
We distinguish the permissions of the application according to Requestcode. When the user chooses, it will callback the interface, we can know the user's choice, and then proceed to the next step.
The examples in this article can be obtained in the following ways,
Https://github.com/KingPaul/DynamicPermissionDemo
Android permission dynamic application, Authorization