一个兼容Kotlin和Java的简单android 动态权限框架

首先是常用的一些权限组合

/**
 * Created by dk on 2018/6/7
 * 常用的一些权限组合
 */
public interface PermissionConsts {
    String[] CONTACTS = {
            Manifest.permission.WRITE_CONTACTS,
            Manifest.permission.GET_ACCOUNTS,
            Manifest.permission.READ_CONTACTS
    };

    String[] PHONE = {
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.WRITE_CALL_LOG,
            Manifest.permission.USE_SIP,
            Manifest.permission.PROCESS_OUTGOING_CALLS,
            Manifest.permission.ADD_VOICEMAIL
    };

    String[] CALENDAR = {
            Manifest.permission.READ_CALENDAR,
            Manifest.permission.WRITE_CALENDAR
    };

    String[] CAMERA = {
            Manifest.permission.CAMERA
    };

    String[] SENSORS = {
            Manifest.permission.BODY_SENSORS
    };

    String[] LOCATION = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };


    String[] STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    String[] MICROPHONE = {
            Manifest.permission.RECORD_AUDIO
    };

    String[] SMS = {
            Manifest.permission.READ_SMS,
            Manifest.permission.RECEIVE_WAP_PUSH,
            Manifest.permission.RECEIVE_MMS,
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.SEND_SMS
    };
}

权限处理结果的接口

 

/**
 * Created by dk on 2018/6/7
 */
public interface IPermissionResult {
    void getPermissionFailed(Activity activity, int requestCode,String[] deniedPermissions);

    void getPermissionSuccess(Activity activity, int requestCode);
}

 

处理权限的类

 

/**
 * Created by dk on 2018/6/7
 */
public class PermissionManager {
    private static volatile PermissionManager instance;
    private static IPermissionResult iPermissionResult = new IPermissionResult() {
        @Override
        public void getPermissionFailed(Activity activity, int requestCode,String[] deniedPermissions) {

        }

        @Override
        public void getPermissionSuccess(Activity activity, int requestCode) {

        }
    };
    private Activity activity;
    private int REQUEST_CODE;
    private static final String TAG = "PermissionManager";

    private PermissionManager() {

    }

    public static PermissionManager init() {
        if (instance == null) {
            synchronized (PermissionManager.class) {
                if (instance == null) {
                    instance = new PermissionManager();
                }
            }
        }
        return instance;
    }

    public PermissionManager checkPermissions(Activity activity, int
            REQUEST_CODE, IPermissionResult result, String[]... permissions) {
        this.activity = activity;
        this.iPermissionResult = result;
        this.REQUEST_CODE = REQUEST_CODE;
        List< String > allPermission = new ArrayList<>();
        for (String[] permission1 : permissions) {
            for (String permission : permission1) {
                allPermission.add(permission);
            }
        }
        getPermission(activity, allPermission);
        return this;
    }


    private void getPermission(Activity activity, List< String > permissions) {
        List< String > applyPermission = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < permissions.size(); i++) {
                String permission = permissions.get(i);
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager
                        .PERMISSION_GRANTED) {
                    applyPermission.add(permission);
                }
            }
            checkPermissionResult(applyPermission);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermissionResult(List< String > list) {
        String[] permissions = list.toArray(new String[list.size()]);
        if (list.size() > 0) {
            Log.d(TAG, "申请权限");
            ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE);
        } else {
            iPermissionResult.getPermissionSuccess(activity, REQUEST_CODE);
        }
    }

    public static void onRequestPermissionsResult(Activity activity, int requestCode, String[] permissions, int[]
            grantResults) {
        requestResult(activity, requestCode, permissions, grantResults);
    }

    private static void requestResult(Activity activity, int code, String[] permissions, int[] results) {
        List< String > deniedPermissions = new ArrayList<>();
        for (int i = 0; i < results.length; i++) {
            if (results[i] != PackageManager.PERMISSION_GRANTED) {
                deniedPermissions.add(permissions[i]);
            }
        }
        if (deniedPermissions.size() > 0) {
            iPermissionResult.getPermissionFailed(activity, code,deniedPermissions.toArray(new String[deniedPermissions.size()]));
        } else {
            iPermissionResult.getPermissionSuccess(activity, code);
        }
    }
}

 

java用法

 

   //首先重写onRequestPermissionsResult方法 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NotNull String[] permissions, @NotNull int[]
            grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.onRequestPermissionsResult(activity, requestCode, permissions, grantResults);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PermissionManager.init().checkPermissions(activity, PermissionConsts.LOCATION, REQUEST_CODE, new
                IPermissionResult() {
                    @Override
                    public void getPermissionFailed(Activity activity, int requestCode, String[] deniedPermissions) {
                        //获取权限失败
                    }

                    @Override
                    public void getPermissionSuccess(Activity activity, int requestCode) {
                        //获取权限成功
                    }
                });
    }


Kotlin调用

 

  //首先重写onRequestPermissionsResult方法
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionManager.onRequestPermissionsResult(activity, requestCode, permissions, grantResults)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        PermissionManager.init().checkPermissions(activity, PermissionConsts.LOCATION, REQUEST_CODE, object : IPermissionResult {
            override fun getPermissionFailed(activity: Activity, requestCode: Int, deniedPermissions: Array<out String>?) {
                //获取权限失败
            }

            override fun getPermissionSuccess(activity: Activity, requestCode: Int) {
                //获取权限成功
            }
        })
    }