android权限申请Permission

前言

Android权限申请之前写过,然后总结了下,但是后来忘记记录了,导致我再需要用的时候又得重来一次,很郁闷,这次就做个记录吧
本文参考我得另篇文章
6.0动态加载权限用PermissionGen

这篇文章主要讲解以下知识点

  • 权限配置
  • 权限类
  • activity中调用
一.权限配置

在app的build.gradle中做库依赖:

    //PermissionGen
    compile 'com.lovedise:permissiongen:0.0.6'
二.权限类PermissionHelper代码
package com.example.pei.textdemo.permission;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;

import java.lang.ref.WeakReference;

import kr.co.namee.permissiongen.PermissionGen;

/**
 * Title:权限配置
 * Description:
 * 

* Created by pei * Date: 2017/9/20 */ public class PermissionHelper { private static final int HANDLER_TAG=123; public static final int NORMAL_PERMISSION = 124; private Context mContext; private String mPermissions[]; private Handler mHandler=new PermissionHandler(PermissionHelper.this); private PermissionHelper() { } private static class Holder { private static PermissionHelper instance = new PermissionHelper(); } public static PermissionHelper getInstance() { return Holder.instance; } /** * 加基本权限 **/ public void checkPermissions(String permissions[],Context context) { this.mPermissions=permissions; this.mContext=context; new Thread(new Runnable() { @Override public void run() { mHandler.sendEmptyMessage(HANDLER_TAG); } }).start(); } private void requestPermissions() { PermissionGen.with((Activity) mContext) .addRequestCode(NORMAL_PERMISSION) .permissions(mPermissions) .request(); } /** * 重写activity的onRequestPermissionsResult方法,并在里面调用此方法 * * @param requestCode * @param permissions * @param grantResults */ public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { PermissionGen.onRequestPermissionsResult((Activity)mContext, requestCode, permissions, grantResults); } //自定义handler类 static class PermissionHandler extends Handler { //弱引用(引用外部类) WeakReference mCls; PermissionHandler(PermissionHelper cls) { //构造弱引用 mCls = new WeakReference(cls); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); //通过弱引用获取外部类. PermissionHelper cls = mCls.get(); //进行非空再操作 if (cls != null) { switch (msg.what){ case HANDLER_TAG: cls.requestPermissions(); removeMessages(HANDLER_TAG); break; default: break; } } } } }

三.activity中调用
package com.example.pei.textdemo.permission;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.example.pei.textdemo.MainActivity;
import com.example.pei.textdemo.R;
import com.example.pei.textdemo.base.AppActivity;
import com.example.pei.textdemo.util.AppUtil;
import com.example.pei.textdemo.util.LogUtil;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * Title:
 * Description:
 * 

* Created by pei * Date: 2017/9/26 */ public class PermissionActivity extends AppActivity{ public static Intent newIndexIntent(Context context) { Intent newIntent = new Intent(context, PermissionActivity.class); return newIntent; } @Override public int getContentViewId() { return R.layout.activity_permission; } @Override protected void initData() { String permissions[]={Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; PermissionHelper.getInstance().checkPermissions(permissions,mContext); } @Override protected void setListener(){ } @PermissionSuccess(requestCode = PermissionHelper.NORMAL_PERMISSION) public void requestSuccess(){ //检测版本 LogUtil.e("=====检测版本======="); startActivity(MainActivity.newIndexIntent(mContext)); } @PermissionFail(requestCode = PermissionHelper.NORMAL_PERMISSION) public void requestFail(){ //不给权限就退出 LogUtil.e("=====失败了======="); AppUtil.goToSetting(mContext); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { PermissionHelper.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults); } }

我这里是以获取读写权限为例,当然,你还得在mainfast中添加读写权限

    
    

ok,今天关于权限得知识就讲到这里吧

你可能感兴趣的:(android权限申请Permission)