Android面试经验程序员Android随笔记

android6.0以上获取权限简单封装

2017-07-13  本文已影响46人  行走的老者

权限辅助类,无需反射,简单封装

/**
 * desc:   权限申请辅助
 * author: jacksonliao
 * email: 583125288@qq.com
 * date: 17/5/26
 */
public class PermissionHelper {
    private int mRequestPermissionCode;
    //处理权限
    private List<String> mRequestPermissionList;
    private OnRequestPermissionCallBack mOnRequestPermissionCallBack;


    public PermissionHelper(OnRequestPermissionCallBack onRequestPermissionCallBack) {
        this.mOnRequestPermissionCallBack = onRequestPermissionCallBack;
    }

    /**
     * Activity中请求权限
     *
     * @param permissions
     * @return true:已经获取了所有请求的权限 false:还没有完全获取权限
     */
    public boolean requestPermissions(Activity activity, int requestCode, String... permissions) {
        if (activity == null) return false;
        if (permissions != null && permissions.length > 0) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mRequestPermissionCode = requestCode;

                if (mRequestPermissionList == null)
                    mRequestPermissionList = new ArrayList<>();
                else
                    mRequestPermissionList.clear();

                for (String permission : permissions) {
                    if (hashPermission(activity, permission)) continue;
                    mRequestPermissionList.add(permission);
                }

                if (mRequestPermissionList.size() == 0) {
                    return true;
                } else {
                    ActivityCompat.requestPermissions(activity,
                            mRequestPermissionList.toArray(new String[mRequestPermissionList.size()]),
                            mRequestPermissionCode);
                    return false;
                }
            } else {
                return true;
            }
        }
        return true;
    }

    /**
     * 在Fragment请求权限
     *
     * @param permissions
     * @return true:已经获取了所有请求的权限 false:还没有完全获取权限
     */
    public boolean requestPermissions(Fragment fragment, int requestCode, String... permissions) {
        if (fragment == null) return false;
        if (permissions != null && permissions.length > 0) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mRequestPermissionCode = requestCode;

                if (mRequestPermissionList == null)
                    mRequestPermissionList = new ArrayList<>();
                else
                    mRequestPermissionList.clear();

                for (String permission : permissions) {
                    if (hashPermission(fragment.getContext(), permission)) continue;
                    mRequestPermissionList.add(permission);
                }

                if (mRequestPermissionList.size() == 0) {
                    return true;
                } else {
                    fragment.requestPermissions(
                            mRequestPermissionList.toArray(new String[mRequestPermissionList.size()]),
                            mRequestPermissionCode);
                    return false;
                }
            } else {
                return true;
            }
        }
        return true;
    }


    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode == mRequestPermissionCode) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED
                        && !ListUtils.isEmpty(mRequestPermissionList)
                        && !TextUtils.isEmpty(permissions[i])
                        && mRequestPermissionList.contains(permissions[i])) {
                    mRequestPermissionList.remove(permissions[i]);
                }
            }

            String[] permissionArray = mRequestPermissionList.toArray(
                    new String[mRequestPermissionList.size()]);

            if (mRequestPermissionList.size() == 0) {
                if (mOnRequestPermissionCallBack != null)
                    mOnRequestPermissionCallBack.requestPermissionSuccess(requestCode, permissionArray);
            } else {
                if (mOnRequestPermissionCallBack != null)
                    mOnRequestPermissionCallBack.requestPermissionFail(requestCode, permissionArray);
            }
        }
    }


    private boolean hashPermission(Context context, String permission) {
        return context != null &&
                ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public void removeCallback() {
        mOnRequestPermissionCallBack = null;
    }

    public interface OnRequestPermissionCallBack {
        void requestPermissionSuccess(int requestPermissionCode, String... permissions);

        void requestPermissionFail(int requestPermissionCode, String... permissions);
    }
}

在Base中封装


/**
 * desc:  具有申请权限的activity
 * author: jacksonliao
 * email: 583125288@qq.com
 * date: 17/5/26
 */
public abstract class BasePermissionActivity extends BaseActivity
        implements PermissionHelper.OnRequestPermissionCallBack {

    private PermissionHelper mPermissionHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mPermissionHelper = new PermissionHelper(this);
        super.onCreate(savedInstanceState);
    }

    /***
     * 请求权限
     * @param requestCode  本次请求吗
     * @param permission 权限集合
     * @return true:有权限  false:无权限
     */
    protected boolean requestPermission(int requestCode, String... permission) {
        return mPermissionHelper.requestPermissions(this, requestCode, permission);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (mPermissionHelper != null)
            mPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void requestPermissionSuccess(int requestPermissionCode, String... permissions) {

    }

    @Override
    public void requestPermissionFail(int requestPermissionCode, String... permissions) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPermissionHelper != null) {
            mPermissionHelper.removeCallback();
            mPermissionHelper = null;
        }
    }
}

使用

继承BasePermissionActivity,调用requestPermission(int,string)请求权限,在requestPermissionSuccess(int requestPermissionCode, String... permissions)与requestPermissionFail(int requestPermissionCode, String... permissions)回调结果即可,示例:

if (requestPermission(PERMISSION_ALBUM_REQUEST_CODE, Manifest.permission.READ_EXTERNAL_STORAGE)) {
            openAlbum();
        }
        
...
 @Override
    public void requestPermissionSuccess(int requestPermissionCode, String... permissions) {
        if (requestPermissionCode == PERMISSION_CAMERA_REQUEST_CODE) {
            openCamera();
        } else if (requestPermissionCode == PERMISSION_ALBUM_REQUEST_CODE) {
            openAlbum();
        }
    }

    @Override
    public void requestPermissionFail(int requestPermissionCode, String... permissions) {
        if (requestPermissionCode == PERMISSION_CAMERA_REQUEST_CODE) {
            ToastUtils.show(mContext, R.string.permission_camera_has);
        } else if (requestPermissionCode == PERMISSION_ALBUM_REQUEST_CODE) {
            ToastUtils.show(mContext, R.string.permission_read_album_has);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读