• 安卓6.0后运行时权限封装


    以SD卡读写权限为例:

    MainActivity继承PermissionActivity,调用时,仅需:

    applyStoragePermission();
    this.setOnPermissionListener(new OnPermissionListener() {
        @Override
        public void ok() {
            //TODO
        }
    });

    这里写图片描述

    申请权限的Activity父类:(此处仅以SD卡为例,其他可以在switch中添加其他)

    package utils.base;
    
    import android.Manifest;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AppCompatActivity;
    
    /**
     * 作者:张风捷特烈
     * 时间:2018/5/16 14:55
     * 邮箱:1981462002@qq.com
     * 说明:申请权限的Activity父类
     */
    public class PermissionActivity extends AppCompatActivity {
        private static final int STORAGE_CODE = 0x01;
        private PermissionBean mStorageModel;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
    
        /**
         * 申请权限
         */
        public void applyPermission(PermissionBean model) {
    
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, model.permission)) {
                ActivityCompat.requestPermissions(this, new String[]{model.permission}, model.requestCode);
                return;
            }
            if (mOnPermissionListener != null) {
                mOnPermissionListener.ok();
            }
    
        }
    
        /**
         * 申请SD卡读写权限
         */
        public void applyStoragePermission() {
            mStorageModel = new PermissionBean(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    "我们需要您允许我们读写你的存储卡,以方便我们临时保存一些数据", STORAGE_CODE);
            applyPermission(mStorageModel);
        }
    
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            switch (requestCode) {
                case STORAGE_CODE:
                    showInfoDialog(permissions, grantResults[0], mStorageModel);
                    break;
            }
        }
    
        /**
         * 如果拒绝,弹出对话框,说明为什么需要这个权限
         *
         * @param permissions
         * @param grantResult
         * @param model
         */
        private void showInfoDialog(String[] permissions, int grantResult, 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) {
                                            applyStoragePermission();
                                        }
                                    }
                            );
                    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;
            }
        }
    
    
        /**
         * 权限申请事件监听
         */
        public interface OnPermissionListener {
            void ok();//成功时回调方法
        }
    
        private OnPermissionListener mOnPermissionListener;
    
        public void setOnPermissionListener(OnPermissionListener onPermissionListener) {
            mOnPermissionListener = onPermissionListener;
        }
    }
    
    
    
  • 相关阅读:
    信息安全系统设计基础第九周学习总结
    信息安全系统设计基础第八周期中总结
    信息安全系统设计基础第七周学习总结
    深入理解计算机系统第六章家庭作业之6.35 6.36
    深入理解计算机系统第二、三章家庭作业之2.64 2.65 3.66 3.68
    信息安全系统设计基础第六周学习总结
    信息安全系统设计基础第五周学习总结
    JAVA 之 多态 抽象 接口
    JAVA 之 继承
    java.lang 类String
  • 原文地址:https://www.cnblogs.com/toly-top/p/9782031.html
Copyright © 2020-2023  润新知