在我们Android6.0以前,我们在开发时候不需要动态的在代码中添加权限,只需要在manifast.xml中配置我们所需要的权限即可,因为在我们安装时,会有一个权限列表,包含了所有我们在manifest.xml中所授予的权限,只有用户同意后才可以继续安装,而在Android6.0之后,我们可以直接安装,当app需要我们授予不恰当的权限的时候,我们可以予以拒绝和接受,当然你也可以在设置界面对每个app的权限进行查看,以及对单个权限进行授权或者解除授权。这样的权限新机制更好的保护了用户的隐私。
Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS
普通权限
时,系统会直接授权通过。
// 针对即使获取了拨打电话的权限依然报错问题的解决方案
int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
// 检查是否获得了权限(Android6.0运行时权限)
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
// 没有获得授权,申请授权
if (ActivityCompat.shouldShowRequestPermissionRationale(mContext,Manifest.permission.CALL_PHONE)) {
// 返回值:
//如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
// 弹窗需要解释为何需要该权限,再次请求授权
Toast.makeText(mContext, "请授权!", Toast.LENGTH_LONG).show();
// 帮跳转到该应用的设置界面,让用户手动授权
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
mContext.startActivity(intent);
}else{
// 不需要解释为何需要该权限,直接请求授权
ActivityCompat.requestPermissions(mContext,new String[]{Manifest.permission.CALL_PHONE},MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
}else {
// 已经获得授权,可以打电话
//用intent启动拨打电话
try {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + 从数据源解析的手机号字段));
mContext.startActivity(intent);
} catch (Exception e) {
}
}
checkSelfPermission
检查权限// 相机权限
public static final int REQUEST_CAMERA = 4;
private static String[] PERMISSIONS_CAMERA = {Manifest.permission.CAMERA};
/**
* 申请相机权限
* @param activity
*/
public static void checkPermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
if (permission != PackageManager.PERMISSION_GRANTED) {
logger.debug("Not have camera permissions.");
ActivityCompat.requestPermissions(activity, PERMISSIONS_CAMERA, REQUEST_CAMERA);
}
}
以上是检查相机权限的代码,在调用的Activity类中,重写如下方法:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == RequestPermissions.REQUEST_CAMERA) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, "请授予相机权限", Toast.LENGTH_SHORT).show();
finish();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
compile 'com.github.hotchemi:permissionsdispatcher:2.3.2'
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.3.2'
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
//Android6.0申请权限的回调方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
点击后跳转如下:
在上方选中所要检查的权限,下方的四个方法全选,Method Name可以随意定义,完成后点击Generate生成方法
@NeedsPermission({Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE})
void permission_need() {
//(必选)当进入Activity类时,如果已授权,会直接调用该注解的方法。
}
@OnShowRationale({Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE})
void permission_show(final PermissionRequest request) {
//(可选)授权申请时,调用。
}
@OnPermissionDenied({Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE})
void permission_deny() {
//(可选)授权申请被拒绝时,调用。
}
@OnNeverAskAgain({Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE})
void permission__never() {
//(可选)授权申请被勾选不再提示时,调用。
}
红体即为Method Name,同时在该Activity上生成注解@RuntimePermissions
//Android6.0申请权限的回调方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
OrderCommentActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
OrderCommentActivityPermissionsDispatcher.getCameraWithCheck(this);
以上是我在对Android权限处理上的一些经验,个人还是推荐使用PermissionsDispatcher,这个插件使用方便,简单;希望以上的这些对看到这篇文章的您有所帮助。