Android6.0权限机制(二):封装

96
08_carmelo
2017.05.10 23:11* 字数 417

本篇文章已授权微信公众号 hongyangAndroid (鸿洋)独家发布
Android6.0权限机制(一):介绍
Android6.0权限机制(二):封装
Android6.0权限机制(三):6.0以前国产手机权限处理

前言

如果项目中用到危险权限的操作很多,那一个个去检查不是累死,当然要封装。

封装权限机制的方法

由于申请权限的回调onRequestPermissionsResult是在Activity或者Fragment的方法,只能用作回调我们没办法自己写个工具类拿到这个回调,那么参考郭霖的权限机制讲解,封装方法有三种:

  1. 自定义一个PermissionActivity,专门用于处理申请运行时权限操作。该Activity背景透明,用户无法察觉。执行完后finish掉。
  2. RxPermision 开源框架:https://github.com/tbruyelle/RxPermissions ,基本思路是透明的Fragment加入到当前的Activity来处理回调,比上面的方法更巧妙,但是这里必须使用RxJava
  3. 封装BaseActivity去实现运行时权限申请方法,然后所有Activity继承BaseActivity,需要时调用方法即可。(推荐)下面采用这一种讲解大概思路

封装BaseActivity

  1. 把权限检测放在BaseActivity中,以接口回调的形式通知检测结果
  2. 权限检测不一定在Activity中,比如在Fragment甚至在某个工具类中,所以BaseActivity中检测权限方法应该是public static,并且通过维护一个Activity栈来获取顶层的Activity

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() {
        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();
        void onDenied(List<String> deniedPermissions);
    }
}

BaseApplication: 注意要在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(){
        if(activityList.isEmpty()){
            return null;
        }
        return activityList.get(activityList.size()-1);
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }
}
Android经验分享
Web note ad 1