First talk about some of the new features of Android 6.0
- Voice Search under lock screen
- Fingerprint identification
- More complete application Rights Management
- Doze Power Management
- Now OnTap
- APP Link
The most important thing in the development process with us is the "more complete application Rights Management" feature, which is what matters most in this area is the runtime permissions,
Run-time permissions
On Android6.0 on the basis of the original Androidmanifest.xml declaration authority, we have added the dynamic monitoring of runtime permissions, the following permissions need to be judged at runtime
- Body Sensors
- Calendar
- Camera
- Contacts
- Location
- Microphone
- Phone
- Sms
- Storage space
Run-time Permission handling
The Android 6.0 system defaults to targetsdkversion less than 23 app grants all the permissions requested, so if your previous app set targetsdkversion less than 23 will not crash at run time, but this is only a temporary strategy, The user may also cancel the authorization in the settings
To declare the target SDK version, we need to declare targetsdkversion as 23 in Build.gradle
Check and Request permissions
We need to use permissions, every time we check whether the app has permissions, such as we have a download function need to write SD card, for example, we have a download function
if (Contextcompat.checkselfpermission (this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
! = packagemanager.permission_granted) {
Request Write_external_storage Permission
Activitycompat.requestpermissions (this, new String[]{manifest.permission.write_external_storage},
Write_external_storage_request_code);
}
When the permission is requested, the system will eject the request permission dialog
The Onrequestpermissionresult method is called after the user chooses to allow or deny, which is similar to Onactivityresult
@Override
public void Onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) {
Super.onrequestpermissionsresult (Requestcode, permissions, grantresults);
}
We then need to follow up with Requestcode and grantresults (authorization results).
Special handling of run-time permissions in fragment
- Apply permission in fragment, do not use activitycompat.requestpermissions, use fragment Requestpermissions method directly, Otherwise it will call back to the activity's Onrequestpermissionsresult
- If you nest fragment in fragment, the Requestpermissions method is used in the sub-fragment, and Onrequestpermissionsresult does not call back. It is recommended to use the Getparentfragment (). Requestpermissions method,
This method will call back to the Onrequestpermissionsresult in the parent fragment and add the following code to pass the callback through to the child fragment
public void Onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) {
Super.onrequestpermissionsresult (Requestcode, permissions, grantresults);
list<fragment> fragments = Getchildfragmentmanager (). getfragments ();
if (fragments! = null) {
for (Fragment fragment:fragments) {
if (fragment! = null) {
Fragment.onrequestpermissionsresult (Requestcode,permissions,grantresults);
}
}
}
}
Related Open source projects
Permissionsdispatcher
Dynamically generated classes handle run-time permissions using annotations, and nested fragment are not currently supported.
Rxpermissions
Rxjava-based runtime permission detection framework
Grant
Simplifies the handling of run-time permissions and is more flexible
Android-runtimepermissions
Google's official example
Android 6.0 new Features