天天看點

Android 權限申請工具類

Android 權限申請工具類

本篇文章用于記錄在項目中用到的權限申請工具類。我們在項目中都遇到過權限申請問題,每一次的權限申請都是繁瑣的,并且在權限的各個狀态都需要我們去判斷,讓每個開發人員都無不煩躁。在這裡我要十分感謝郭霖大神的部落格PermissionX現在支援Java了!還有Android 11權限變更講解這篇文章為我們封裝了很好的權限申請功能。

內建:

使用:

PermissionX.init(fragmentActivity)
                .permissions(Manifest.permission.CAMERA)
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                    //這裡是選擇禁止後的提示
                        scope.showRequestReasonDialog(deniedList, "相機權限是必須依賴的權限", "去開啟","關閉");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                    //這裡是選擇禁止且不再提示後的提示
                        scope.showForwardToSettingsDialog(deniedList, "程式需要您去應用程式設定當中手動開啟相機權限", "去開啟","關閉");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                           //這裡是申請權限同意後的操作
                        }else {
                           //不同意時的操作
                        }

                    }
                });
           

上面是郭霖大神封裝後的使用方法,很簡便。在實際項目使用中,我發現光是這樣還不夠,我根據自己項目的業務邏輯在上面的基礎上封裝成了工具類,直接調用即可。

使用情景1、申請的權限不是必須權限,不管是否同意都能繼續往下走

工具類實作:

/**
     * 此方法申請的權限不是必須的,就是不管是否同意權限都能繼續往下走
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                    	//不管同不同意權限,都繼續往下執行
                        if (afterPermission!=null){
                            afterPermission.doNext();
                        }
                    }
                });
    }
           

外部調用:

PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermission(this, new PermissionUtils.AfterPermission() {
            @Override
            public void doNext() {
                //此處調用繼續往下執行的代碼
            }
        },Manifest.permission.WRITE_EXTERNAL_STORAGE);
           
使用情景2、此方法申請的權限不是必須的,但在不同意權限的時候會走另外一條路

工具類中方法實作:

/**
     * 此方法申請的權限不是必須的,但在不同意權限的時候會走另外一條路
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,boolean other,AfterPermissionTwo afterPermissionTwo,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                        	//同意權限
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.doNext();
                            }
                        }else {
                        	//不同意權限
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.toOther();
                            }
                        }

                    }
                });
    }
           

實際使用:

PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermission(getActivity(),true,new PermissionUtils.AfterPermissionTwo() {
            @Override
            public void doNext() {
                //同意權限
            }
            @Override
            public void toOther() {
                //不同意權限
            }
        },Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
           
使用情景3、此方法申請的權限是必須的,必須同意權限都能繼續往下走

工具類實作:

/**
     * 此方法申請的權限是必須的,必須同意權限都能繼續往下走
     * @param fragmentActivity
     * @param msg1 申請的權限名稱
     * @param msg2  哪個功能需要申請權限
     * @param afterPermission   監聽權限設定接口
     * @param permissions   權限清單
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"權限是"+msg2+"必須依賴的權限", "去開啟","關閉");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去應用程式設定當中手動開啟"+msg1+"權限", "去開啟","關閉");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                        	//同意權限後才能繼續走
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }

                    }
                });
    }
           

實際調用:

PermissionUtils permissionUtils = new PermissionUtils();
                permissionUtils.setPermission(this, "存儲", "儲存圖檔", new PermissionUtils.AfterPermission() {
                    @Override
                    public void doNext() {
                        //同意權限後往下執行
                    }
                },Manifest.permission.WRITE_EXTERNAL_STORAGE);
           
使用情景4、此方法申請的權限是必須的,必須同意權限都能繼續往下走,不同意會走另外一條路

工具類方法實作:

/**
     * 此方法申請的權限是必須的,必須同意權限都能繼續往下走,不同意會走另外一條路
     * @param fragmentActivity
     * @param msg1 申請的權限名稱
     * @param msg2  哪個功能需要申請權限
     * @param afterPermission   監聽權限設定接口
     * @param permissions   權限清單
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermissionTwo afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"權限是"+msg2+"必須依賴的權限", "去開啟","關閉");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去應用程式設定當中手動開啟"+msg1+"權限", "去開啟","關閉");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                        //同意權限
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }else {
                        //不同意權限
                            if (afterPermission!=null){
                                afterPermission.toOther();
                            }
                        }

                    }
                });
    }
           

實際使用:

PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermission(this,"定位","選擇位置",new PermissionUtils.AfterPermissionTwo() {
            @Override
            public void doNext() {
                //同意權限繼續走
            }
            @Override
            public void toOther() {
                //不同意走另一條路
            }
        },Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
           

最後給大家複制一下總的工具類:

/**
 * created by DELL
 * on 2020/7/29
 * 權限申請工具類
 */
public class PermissionUtils {
    /**
     * 此方法申請的權限不是必須的,就是不管是否同意權限都能繼續往下走
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (afterPermission!=null){
                            afterPermission.doNext();
                        }
                    }
                });
    }
    /**
     * 此方法申請的權限不是必須的,但在不同意權限的時候會走另外一條路
     * @param fragmentActivity
     * @param permissions
     */
    public void setPermission(FragmentActivity fragmentActivity,boolean other,AfterPermissionTwo afterPermissionTwo,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.doNext();
                            }
                        }else {
                            if (afterPermissionTwo!=null){
                                afterPermissionTwo.toOther();
                            }
                        }

                    }
                });
    }

    /**
     * 此方法申請的權限是必須的,必須同意權限都能繼續往下走
     * @param fragmentActivity
     * @param msg1 申請的權限名稱
     * @param msg2  哪個功能需要申請權限
     * @param afterPermission   監聽權限設定接口
     * @param permissions   權限清單
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermission afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"權限是"+msg2+"必須依賴的權限", "去開啟","關閉");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去應用程式設定當中手動開啟"+msg1+"權限", "去開啟","關閉");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }else {

                        }

                    }
                });
    }
    /**
     * 此方法申請的權限是必須的,必須同意權限都能繼續往下走,不同意會走另外一條路
     * @param fragmentActivity
     * @param msg1 申請的權限名稱
     * @param msg2  哪個功能需要申請權限
     * @param afterPermission   監聽權限設定接口
     * @param permissions   權限清單
     */
    public void setPermission(FragmentActivity fragmentActivity,String msg1,String msg2,AfterPermissionTwo afterPermission,String... permissions){
        PermissionX.init(fragmentActivity)
                .permissions(new ArrayList<>(Arrays.asList(permissions)))
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, msg1+"權限是"+msg2+"必須依賴的權限", "去開啟","關閉");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, msg2+"需要您去應用程式設定當中手動開啟"+msg1+"權限", "去開啟","關閉");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted){
                            if (afterPermission!=null){
                                afterPermission.doNext();
                            }
                        }else {
                            if (afterPermission!=null){
                                afterPermission.toOther();
                            }
                        }

                    }
                });
    }

    /**
     * 定義申請權後下一步操作接口
     */
    public interface AfterPermission{
        void doNext();
    }
    public interface AfterPermissionTwo{
        void doNext();  //權限同意
        void toOther();  //權限不同意
    }
           

本工具類已經運用到項目中,本篇文章用作記錄,也歡迎大家糾錯。