安卓6.0后运行时权限封装

mac2022-06-30  120

以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; } }

转载于:https://www.cnblogs.com/toly-top/p/9782031.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)