TI--安卓运行时权限完美封装

作者: e4e52c116681 | 来源:发表于2018-08-25 16:52 被阅读9次

    零、前言:

    [1]两行代码解决运行时权限
    [2]用户拒绝后显示提示信息,有再次验证功能
    [3]验证成功回调

    一个权限效果:
    一个权限效果.gif
    六个权限演示:
    六个权限演示.gif
    一、代码实现
    1.单一权限申请
    public class MainActivity extends PermissionActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //单权限申请
            applyPermissions(_WRITE_EXTERNAL_STORAGE());
        }
        
        @Override
        protected void permissionOk() {
            ToastUtil.showAtOnce(MainActivity.this, "授权通过");
        }
    }
    
    2.多权限:常用的六组运行时权限
    applyPermissions(
    _WRITE_EXTERNAL_STORAGE(), _READ_CONTACTS(),
    _CALL_PHONE(), _CAMERA(), _READ_SMS(), _RECORD_AUDIO());
    

    本文由张风捷特烈原创,转载请注明
    更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94
    张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
    你的喜欢与支持将是我最大的动力


    附录一:权限封装类,拷贝即用
    /**
     * 作者:张风捷特烈
     * 时间:2018/5/16 14:55
     * 邮箱:1981462002@qq.com
     * 说明:申请权限的Activity父类
     * <p>
     * 用法:1:继承 PermissionActivity
     * 2:调用applyPermissions
     * 3:permissionOk()回调
     */
    public abstract class PermissionActivity extends AppCompatActivity {
        private static final int WRITE_EXTERNAL_STORAGE = 0x01;
        private static final int RECORD_AUDIO = 0x02;
        private static final int CAMERA = 0x03;
        private static final int READ_CONTACTS = 0x04;
        private static final int CALL_PHONE = 0x05;
        private static final int READ_SMS = 0x06;
        private PermissionBean mStorageModel, mRecorderModel, mCameraModel, mContactsModel, mCallPhoneModel, mSMSModel;
        private PermissionBean[] mModels;
        private int count = 0;
        protected boolean noPerm = true;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        /**
         * 申请权限
         */
        private void applyPermission(PermissionBean model) {
    
            noPerm = PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, model.permission);
            if (noPerm) {
                ActivityCompat.requestPermissions(this, new String[]{model.permission}, model.requestCode);
                count++;
            }
        }
    
        /**
         * 申请权限
         */
        public void applyPermissions(PermissionBean... models) {
            mModels = models;
            System.out.println(count);
            applyPermission(models[count]);
            if (!noPerm) {
                permissionOk();
            }
        }
    
        /**
         * 申请SD卡读写权限
         */
        public PermissionBean _WRITE_EXTERNAL_STORAGE() {
            mStorageModel = new PermissionBean(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    "我们需要您允许我们读写你的存储卡,以方便使用该功能!", WRITE_EXTERNAL_STORAGE);
            return mStorageModel;
        }
    
        /**
         * 申请读取通讯录权限
         */
        public PermissionBean _READ_CONTACTS() {
            mContactsModel = new PermissionBean(Manifest.permission.READ_CONTACTS,
                    "我们需要您允许我们读取通讯录,以方便功能使用!", READ_CONTACTS);
            return mContactsModel;
        }
    
        /**
         * 申请读取通讯录权限
         */
        public PermissionBean _READ_SMS() {
            mSMSModel = new PermissionBean(Manifest.permission.READ_SMS,
                    "我们需要您允许我们读取短信,以方便功能使用!", READ_SMS);
            return mSMSModel;
        }
    
        /**
         * 申请录音权限
         */
        public PermissionBean _RECORD_AUDIO() {
            mRecorderModel = new PermissionBean(Manifest.permission.RECORD_AUDIO,
                    "我们需要您允许我们访问录音设备,以方便录音功能使用!", RECORD_AUDIO);
            return mRecorderModel;
        }
    
        /**
         * 申请拍照权限
         */
        public PermissionBean _CAMERA() {
            mCameraModel = new PermissionBean(Manifest.permission.CAMERA,
                    "我们需要您允许我们访问相机,以方便拍照功能使用!", CAMERA);
            return mCameraModel;
        }
    
        /**
         * 申请电话权限
         */
        public PermissionBean _CALL_PHONE() {
            mCallPhoneModel = new PermissionBean(Manifest.permission.CALL_PHONE,
                    "我们需要您允许我们访问电话设备,以方便功能使用!", CALL_PHONE);
            return mCallPhoneModel;
        }
    
        /**
         * 当用户处理完授权操作时,系统会自动回调该方法
         *
         * @param requestCode
         * @param permissions
         * @param grantResults
         */
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                if (count != mModels.length) {
                    applyPermission(mModels[count]);
                } else {
                    permissionOk();
                    count=0;//count归零
                }
            } else {
                count--;
            }
    
    
            switch (requestCode) {
                case WRITE_EXTERNAL_STORAGE:
                    showInfoDialog(permissions, grantResults[0], mStorageModel);
                    break;
                case RECORD_AUDIO:
                    showInfoDialog(permissions, grantResults[0], mRecorderModel);
                    break;
                case CAMERA:
                    showInfoDialog(permissions, grantResults[0], mCameraModel);
                    break;
                case READ_CONTACTS:
                    showInfoDialog(permissions, grantResults[0], mContactsModel);
                    break;
                case CALL_PHONE:
                    showInfoDialog(permissions, grantResults[0], mCallPhoneModel);
                    break;
                case READ_SMS:
                    showInfoDialog(permissions, grantResults[0], mSMSModel);
                    break;
            }
    
        }
    
        /**
         * 如果拒绝,弹出对话框,说明为什么需要这个权限
         *
         * @param permissions
         * @param grantResult
         * @param model
         */
        private void showInfoDialog(String[] permissions, int grantResult, final PermissionBean model) {
            if (PackageManager.PERMISSION_GRANTED != grantResult) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("权限申请")
                            .setMessage(model.explain).setPositiveButton(
                                    "我知道了",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            applyPermission(model);
                                        }
                                    }
                            );
                    builder.setCancelable(false);
                    builder.show();
                }
            }
            return;
        }
    
        /**
         * Permission的bean对象
         */
        public static class PermissionBean {
    
            public String permission;//请求的权限
            public String explain;//解析为什么请求这个权限
            public int requestCode;//请求代码
    
            public PermissionBean(String permission, String explain, int requestCode) {
                this.permission = permission;
                this.explain = explain;
                this.requestCode = requestCode;
            }
    
        }
    
        protected abstract void permissionOk();
    }
    
    附录二:危险权限:需要运行时
    group:android.permission-group.CONTACTS 通讯录方面 
    permission:android.permission.WRITE_CONTACTS 写入通讯录 
    permission:android.permission.GET_ACCOUNTS 访问通讯录权限 
    permission:android.permission.READ_CONTACTS 读取通讯录
    
    group:android.permission-group.PHONE 电话方面 
    permission:android.permission.READ_CALL_LOG 看电话记录 
    permission:android.permission.READ_PHONE_STATE 读取手机状态 
    permission:android.permission.CALL_PHONE 打电话 
    permission:android.permission.WRITE_CALL_LOG 编写调用日志 
    permission:android.permission.USE_SIP 使用SIP 
    permission:android.permission.PROCESS_OUTGOING_CALLS 过程输出调用 permission:com.android.voicemail.permission.ADD_VOICEMAIL 添加语音信箱
    
    group:android.permission-group.CALENDAR 日历 
    permission:android.permission.READ_CALENDAR 读取日历 
    permission:android.permission.WRITE_CALENDAR 写入日历 
    
    group:android.permission-group.CAMERA 照相机 
    permission:android.permission.CAMERA
    
    group:android.permission-group.SENSORS 传感器 
    permission:android.permission.BODY_SENSORS 体传感器
    
    group:android.permission-group.LOCATION 位置 
    permission:android.permission.ACCESS_FINE_LOCATION 获取好位置 
    permission:android.permission.ACCESS_COARSE_LOCATION 
    
    获取粗定位group:android.permission-group.STORAGE 存储 
    permission:android.permission.READ_EXTERNAL_STORAGE 读取外部存储器
    permission:android.permission.WRITE_EXTERNAL_STORAGE 写外部存储器
    
    group:android.permission-group.MICROPHONE 扩音器;麦克风 
    permission:android.permission.RECORD_AUDIO 录音
    
    group:android.permission-group.SMS 信息 
    permission:android.permission.READ_SMS 读取信息 
    permission:android.permission.RECEIVE_WAP_PUSH 收到WAP推送 
    permission:android.permission.RECEIVE_MMS 接收彩信 
    permission:android.permission.RECEIVE_SMS 收信息 
    permission:android.permission.SEND_SMS 发信息 
    
    运行时权限组中的一个获取权限,其他就不需要运行时权限,但AndroidManifest.xml仍要配置
    

    相关文章

      网友评论

        本文标题:TI--安卓运行时权限完美封装

        本文链接:https://www.haomeiwen.com/subject/haegiftx.html