Android权限管理工具类

Posted dorr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android权限管理工具类相关的知识,希望对你有一定的参考价值。

package cc.vv.btongbaselibrary.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
* 权限处理
* Created by nanPengFei on 2016/12/21 14:11.
*/
public class LKPermissionUtil {
private static LKPermissionUtil mInstance;

private LKPermissionUtil() {
}

public static LKPermissionUtil getInstance() {
if (null == mInstance) {
synchronized (LKPermissionUtil.class) {
if (null == mInstance) {
mInstance = new LKPermissionUtil();
}
}
}
return mInstance;
}


/**
* 判断是否拥有某种权限,第一次没有则弹窗询问,用户拒绝后,
* 请到对应的Activity中重写onRequestPermissionsResult进行判断requestCode值进行相应提示
*
* @param activity
* @param permission
* @param requestCode
* @return
*/
private boolean requestPermission(Activity activity, String[] permission, int requestCode) {
boolean isHasPermission = false;//默认没有权限
if (ContextCompat.checkSelfPermission(activity, permission[0]) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, permission, requestCode);
} else {
isHasPermission = true;
}
return isHasPermission;
}

/*关于权限申请*/
public static final int REQUEST_PERMISSION_CAMERA = 501;//相机权限申请
public static final int REQUEST_PERMISSION_SD = 502;//SD卡权限申请
public static final int REQUEST_PERMISSION_AUDIO = 503;//录音权限申请
public static final int REQUEST_PERMISSION_LOCATION = 504;//定位权限申请
public static final int REQUEST_PERMISSION_FILESYSTEMS = 505;//挂载SD卡权限申请
public static final int REQUEST_PERMISSION_CONTACTS = 506;//获取读取手机通讯录
//获取拨打电话权限
public static final int REQUEST_PERMISSION_CALL = 507;

/**
* 相机权限申请
*
* @param activity
* @return
*/
public boolean requestCamera(Activity activity) {
return requestPermission(activity,
new String[]{Manifest.permission.CAMERA
}, REQUEST_PERMISSION_CAMERA);
}

/**
* SD卡权限申请
*
* @param activity
* @return
*/
public boolean requestSD(Activity activity) {
return requestPermission(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, REQUEST_PERMISSION_SD);
}

/**
* 录音权限申请
*
* @param activity
* @return
*/
public boolean requestAudio(Activity activity) {
return requestPermission(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO
}, REQUEST_PERMISSION_AUDIO);
}

/**
* 定位权限申请
*
* @param activity
* @return
*/
public boolean requestLocation(Activity activity) {
return requestPermission(activity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, REQUEST_PERMISSION_LOCATION);
}

/**
* 联系人权限申请
*
* @param activity
* @return
*/
public boolean requestContacts(Activity activity) {
return requestPermission(activity,
new String[]{Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS
}, REQUEST_PERMISSION_CONTACTS);
}

/**
* 挂载SD卡权限申请
*
* @param activity
* @return
*/
public boolean requestFilesystems(Activity activity) {
return requestPermission(activity,
new String[]{Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
}, REQUEST_PERMISSION_FILESYSTEMS);
}

/**
* 获取拨打电话权限
*/
public boolean requestCallPhone(Activity activity) {
return requestPermission(activity, new String[]{Manifest.permission.CALL_PHONE},
REQUEST_PERMISSION_CALL);
}

/**
* 申请提供的所有权限
*
* @param activity
*/
public void requestAll(Activity activity) {
requestPermission(activity,
new String[]{Manifest.permission.CAMERA
, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
Manifest.permission.CALL_PHONE},
REQUEST_PERMISSION_CAMERA);
}
}
































































































































































以上是关于Android权限管理工具类的主要内容,如果未能解决你的问题,请参考以下文章

Android PermissionUtils:运行时权限工具类及申请权限的正确姿势

Android 动态申请权限工具类

Android 动态申请权限工具类

Android 权限常量保存在哪个类中

Android权限管理工具类

android权限判断与申请封装类