码迷,mamicode.com
首页 > 移动开发 > 详细

Android权限管理工具类

时间:2018-05-02 16:16:10      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:stc   else   requests   class   self   new   man   手机   工具   

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权限管理工具类

标签:stc   else   requests   class   self   new   man   手机   工具   

原文地址:https://www.cnblogs.com/Dorr/p/8979825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!