1. 程式人生 > >android6.0許可權通用管理(不在頭大)

android6.0許可權通用管理(不在頭大)

一個類解決Android6.0許可權的繁雜,完美應付日常開發,歡迎指教~

 在activity中涉及到需要申請許可權的功能時,比如點選按鈕照相,這是主要一段程式碼,沒什麼難度就是做了一個統一回調:

  /***
         * request permission for activity's method of up 6.0
         * @param permissions
         * @param listener
         */
        public void requestPermissionFeature(String[] permissions, PermissionListener listener) {
            if (activity == null)
                return;
            shouldRequestPermissions = new ArrayList<>();
            denied = new ArrayList<>();
            tips = new ArrayList<>();
            this.permissionListener = listener;
            if (isNeedRequestPermission()) {
                for (int i = 0; i < permissions.length; i++) {
                    if (ContextCompat.checkSelfPermission(activity, permissions[i]) != PackageManager.PERMISSION_GRANTED)
                        shouldRequestPermissions.add(permissions[i]);
                }
                if (shouldRequestPermissions.size() > 0) {
                    String[] requestPermissions = shouldRequestPermissions.toArray(new String[shouldRequestPermissions.size()]);
                    ActivityCompat.requestPermissions(activity, requestPermissions, PERMISSIONS_REQUEST_CODE);
                } else {
                    if (permissionListener != null)
                        permissionListener.granted();

                }

            } else {
                if (permissionListener != null)
                    permissionListener.granted();
            }
        }
在activity中這樣用,同意回撥,拒絕回撥,拒絕後不再提示回撥(需要使用者手動去設定開啟),注意:如果這個功能需要多個許可權,有一個使用者沒有通過,那麼該功能不可用,大家根據自己的需求在不同的回撥裡做操作:
    private void permissget() {   
        builder.activity(this).requestPermissionFeature(new String[]{Manifest.permission.READ_PHONE_STATE}, new Permission.PermissionListener() {
            @Override
            public void granted() {
                initInfo();
            }
            @Override
            public void deniedNeverAsk() {
                ToastUtils.showToast(WelcomeActivity.this,getString(R.string.deniedNeverAsk));
            }
            @Override
            public void denied() {
                finish();
            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        builder.RequestPermissionsResult(requestCode,permissions,grantResults);
    }
在fragment中差不多的用法  把上面的builder.activity(this)換成builder.fragment (this), 方法名requestPermissionForFragment,我會把全部類貼出來直接拿去用就ok了!!!

如果有什麼問題還請指出。。後面會有更多野路子來應付實際開發(都是坑啊)

完整類↓↓

public class Permission {

    public Permission() {
    }
    public  static class Builder {
        private List<String> shouldRequestPermissions;
        private List<Integer> denied;
        private List<Boolean> tips;
        private Activity activity;
        private Fragment fragment;
        private PermissionListener permissionListener;
        private final int PERMISSIONS_REQUEST_CODE = 42;

        public Builder() {

        }

        public Permission build() {
            return new Permission();
        }

        public Builder activity(Activity activity) {
            this.activity = activity;
            return this;
        }

        public Builder fragment(Fragment fragment) {
            this.fragment = fragment;
            return this;
        }
        /***
         * request permission for activity's method of up 6.0
         * @param permissions
         * @param listener
         */
        public void requestPermissionFeature(String[] permissions, PermissionListener listener) {
            if (activity == null)
                return;
            shouldRequestPermissions = new ArrayList<>();
            denied = new ArrayList<>();
            tips = new ArrayList<>();
            this.permissionListener = listener;
            if (isNeedRequestPermission()) {
                for (int i = 0; i < permissions.length; i++) {
                    if (ContextCompat.checkSelfPermission(activity, permissions[i]) != PackageManager.PERMISSION_GRANTED)
                        shouldRequestPermissions.add(permissions[i]);
                }
                if (shouldRequestPermissions.size() > 0) {
                    String[] requestPermissions = shouldRequestPermissions.toArray(new String[shouldRequestPermissions.size()]);
                    ActivityCompat.requestPermissions(activity, requestPermissions, PERMISSIONS_REQUEST_CODE);
                } else {
                    if (permissionListener != null)
                        permissionListener.granted();

                }

            } else {
                if (permissionListener != null)
                    permissionListener.granted();
            }
        }

        /***
         * request permission for fragment's method of up 6.0
         * @param permissions
         * @param listener
         */
        public void requestPermissionForFragment(String[] permissions, PermissionListener listener) {
            if (fragment == null)
                return;
            shouldRequestPermissions = new ArrayList<>();
            denied = new ArrayList<>();
            tips = new ArrayList<>();
            this.permissionListener = listener;
            if (isNeedRequestPermission()) {
                for (int i = 0; i < permissions.length; i++) {
                    if (ContextCompat.checkSelfPermission(fragment.getContext(), permissions[i]) != PackageManager.PERMISSION_GRANTED)
                        shouldRequestPermissions.add(permissions[i]);
                }
                if (shouldRequestPermissions.size() > 0) {
                    String[] requestPermissions = shouldRequestPermissions.toArray(new String[shouldRequestPermissions.size()]);

                    fragment.requestPermissions(requestPermissions, PERMISSIONS_REQUEST_CODE);
                } else {
                    if (permissionListener != null)
                        permissionListener.granted();
                }

            } else {
                if (permissionListener != null)
                    permissionListener.granted();
            }
        }

        /**
         * this callback of all request permission
         * @param requestCode
         * @param permissions
         * @param grantResults
         */
        public void RequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode != PERMISSIONS_REQUEST_CODE)
                return;
            for (int i = 0; i < permissions.length; i++) {
                boolean isTip=false;
                if (activity != null) {
                    isTip = ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[i]);
                } else {
                    isTip = ActivityCompat.shouldShowRequestPermissionRationale(fragment.getActivity(), permissions[i]);
                }
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    denied.add(i);
                    if (!isTip)
                        tips.add(isTip);
                }
            }
            if (denied.size() > 0) {
                if (permissionListener != null) {
                    if (tips.size() > 0)
                        permissionListener.deniedNeverAsk();
                    else
                        permissionListener.denied();
                }

            } else {
                if (permissionListener != null)
                    permissionListener.granted();
            }
        }
    }

    private static boolean isNeedRequestPermission() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? true : false;
    }

    public interface PermissionListener {
        void granted();

        void denied();

        void deniedNeverAsk();
    }

}