This article has authorized wechat public account hongyangAndroid (Hongyang) exclusive release Android6.0 permission mechanism (a) : introduce Android6.0 permission mechanism (two) : package Android6.0 permission mechanism (three) : 6.0 before domestic mobile phone permission processing

preface

If the project uses a lot of dangerous permissions, it is not exhausting to check one by one, of course, to encapsulate.

A way to encapsulate permission mechanisms

Due to apply for permission to callback onRequestPermissionsResult is the Activity or fragments method, we can’t write their own can only be used as a callback as a utility class to get the callback, then explain the reference Guo Lin permissions mechanism, there are three kinds of packaging methods:

  1. Define a custom PermissionActivity that handles requests for runtime permissions. The Activity has a transparent background that the user cannot detect. Finish drops after execution.
  2. RxPermision Open Source Framework: https://github.com/tbruyelle/RxPermissions, the basic idea is transparent fragments to join to the current Activity to handle the callback, more clever than the above methods, but must use RxJava here
  3. Encapsulate BaseActivity to implement the runtime permission request method, and then all activities inherit BaseActivity, calling the method as needed. (Recommended) The following uses this kind of explanation general idea

Encapsulation BaseActivity

  1. Put the permission detection in BaseActivity and notify the detection result in the form of interface callbacks
  2. Permission detection does not have to be in an Activity, such as in a Fragment or even in a tool class, so the BaseActivity permission detection method should be public static and obtain the top-level Activity by maintaining a stack of activities

BaseActivity:


/** * Created by carmelo on 17/3/19. */
public class BaseActivity extends Activity{
    private static OnPermissionCallback callback;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.addActivity(this);
    }
    @Override
    protected void onDestroy(a) {
        super.onDestroy();
        BaseApplication.removeActivity(this);
    }
    public static void requestPermission(String[] permissions, OnPermissionCallback onPermissionCallback){
        if(BaseApplication.getTopAcitivity()==null) {return;
        }
        callback = onPermissionCallback;
        List<String> permissionsList = new ArrayList<>();
        for(String permission:permissions){
            if(ContextCompat.checkSelfPermission(BaseApplication.getTopAcitivity(),permission)!= PackageManager.PERMISSION_GRANTED){
                permissionsList.add(permission);
            }
        }
        if(! permissionsList.isEmpty()){ ActivityCompat.requestPermissions(BaseApplication.getTopAcitivity(),permissionsList.toArray(new String[permissionsList.size()]),1);
        }else {
            if(callback! =null){ callback.onGranted(); }}}@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length>0){
            List<String> deniedPermissions = new ArrayList<>();
            for(int i=0; i<grantResults.length; i++){if(grantResults[i]!=PackageManager.PERMISSION_GRANTED){
                    deniedPermissions.add(permissions[i]);
                }
            }
            if(deniedPermissions.isEmpty()){
                if(callback! =null){ callback.onGranted(); }}else{
                if(callback! =null){ callback.onDenied(deniedPermissions); }}}}public interface OnPermissionCallback{
        void onGranted(a);
        void onDenied(List<String> deniedPermissions); }}Copy the code

BaseApplication: Note that it is registered in the Manifest

public class BaseApplication extends Application {
    public static List<BaseActivity> activityList = new ArrayList<>();
    public static void addActivity(BaseActivity activity){
        activityList.add(activity);
    }
    public static void removeActivity(BaseActivity activity){
        activityList.remove(activity);
    }
    public static BaseActivity getTopAcitivity(a){
        if(activityList.isEmpty()){
            return null;
        }
        return activityList.get(activityList.size()-1);
    }
    @Override
    public void onCreate(a) {
        super.onCreate(); }}Copy the code