欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android6.0权限实用封装

程序员文章站 2022-06-05 14:22:56
...

android6.0权限实用封装

解决6.0权限使用麻烦问题,封装在BaseActivity里面,只需调用请求权限方法,传入权限数组与回调接口即可,对于拒绝权限、不再询问等做了封装处理,使用便捷。
适用于各个场景,例如:应用启动时获取必须权限,拍照或拨打电话调起权限。

代码主要在三个类里面,BaseActivity、BaseFragment、PermissionsListener ,直接上代码:

PermissionsListener 代码

/**
 * 6.0权限封装接口
 */
public interface PermissionsListener {
    void onGranted();

    /**
     * @param deniedPermissions 被拒绝的权限集合
     * @param isNeverAsk    是否所有被拒绝权限都勾选不再询问
     */
    void onDenied(List<String> deniedPermissions, boolean isNeverAsk);
}

BaseActivity代码

public abstract class BaseActivity extends AppCompatActivity {
    public BaseActivity mActivity;
    private PermissionsListener mListener;
    private final int PERMISSION_REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
    }

    /**
     * 请求权限封装
     *
     * @param permissions
     * @param listener
     */
    public void requestPermissions(String[] permissions, PermissionsListener listener) {
        mListener = listener;
        List<String> requestPermissions = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED) {
                requestPermissions.add(permission);
            }
        }
        if (!requestPermissions.isEmpty() && Build.VERSION.SDK_INT >= 23) {
            ActivityCompat.requestPermissions(this, requestPermissions.toArray(new String[requestPermissions.size()]), PERMISSION_REQUEST_CODE);
        } else {
            if (mListener != null) {
                try {
                    mListener.onGranted();
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                //存放被拒绝的权限集合
                List<String> deniedPermissions = new ArrayList<>();
                //当所有拒绝的权限都勾选不再询问,这个值为true,这个时候可以引导用户手动去授权。
                boolean isNeverAsk = true;
                for (int i = 0; i < grantResults.length; i++) {
                    int grantResult = grantResults[i];
                    String permission = permissions[i];
                    if (grantResult == PackageManager.PERMISSION_DENIED) {
                        deniedPermissions.add(permissions[i]);
                        // 点击拒绝但没有勾选不再询问
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                            isNeverAsk = false;
                        }
                    }
                }
                //被拒绝权限为空 则表示所有权限都已获取
                if (mListener != null) {
                    if (deniedPermissions.isEmpty()) {
                        //try catch 避免接口内有异常导致应用崩溃 可不加
                        try {
                            mListener.onGranted();
                        } catch (RuntimeException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            mListener.onDenied(deniedPermissions, isNeverAsk);
                        } catch (RuntimeException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
}

BaseFragment代码

public abstract class BaseFragment extends Fragment {

    public BaseActivity mActivity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getActivity() instanceof BaseActivity){
            mActivity = (BaseActivity) getActivity();
        }
    }

    /**
     * 请求权限封装
     *
     * @param permissions
     * @param listener
     */
    public void requestPermissions(String[] permissions, PermissionsListener listener) {
        if (mActivity==null){
            new RuntimeException("please inheritance BaseActivity");
            return;
        }
        mActivity.requestPermissions(permissions,listener);
    }
}

具体使用请看demo https://github.com/xiejingwen/AndroidPermission