android运⾏时权限代码,⼀⾏代码解决AndroidM新的运⾏时权
限问题
Android M运⾏时权限是个啥东西
啥是运⾏时权限呢?Android M对权限管理系统进⾏了改版,之前我们的App需要权限,只需在manifest中申明即可,⽤户安装后,⼀切申明的权限都可来去⾃如的使⽤。但是Android M把权限管理做了加强处理,在manifest申明了,在使⽤到相关功能时,还需重新授权⽅可使⽤。当然,不是所有权限都需重新授权,所以就把这些需要重新授权⽅可使⽤的权限称之为运⾏时权限。
运⾏时权限的影响
运⾏时权限的好处可以让⽤户使⽤时更有主动权,不会让app随便乱来。
但是受害最深的却是我们这些受苦受难的开发者,为何这么说呢?如果你的app的targetSdkVersion 是23也就是android 6.0的话,遇到运⾏时权限不去做代码处理的话,程序直接崩掉。
所以,如果你的app没有在android 6.0上做⾜够的测试,请不要设置targetSdk为23,22以下就不会出现问题。
但是,作为开发者,我们还是必须要与时俱进的不是,⽽且以后的主流机型也必然是android M,怎么能不去适配他/她呢?继续往下看。
哪些是运⾏时权限
光滑
要先做好适配,那就必然要先了解哪些是运⾏时权限。
先看下哪些是不⽤特殊处理的权限,android称之为普通权限:
As of API level 23, the following permissions are classified as PROTECTION_NORMAL:
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
contagious
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
我叫金三顺插曲
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT吝啬什么意思
com.android.launcher.permission.UNINSTALL_SHORTCUT
危险权限表
同⼀组的任何⼀个权限被授权了,其他权限也⾃动被授权。例如,⼀旦WRITE_CONTACTS被授权了,app也有READ_CONTACTS和GET_ACCOUNTS了。
------------------------------ 关键部分来了---------------------------------------------
代码封装
封装代码如下:
在BaActivity添加如下代码:
//**************** Android M Permission (Android 6.0权限控制代码封装)
*****************************************************
private int permissionRequestCode = 88;
private PermissionCallback permissionRunnable ;
public interface PermissionCallback{
void hasPermission();
void noPermission();
}
/**
* Android M运⾏时权限请求封装
* @param permissionDes 权限描述
* @param runnable 请求权限回调
* @param permissions 请求的权限(数组类型),直接从Manifest中读取相应的值,⽐如Manifest.permission.WRITE_CONTACTS
*/
public void performCodeWithPermission(@NonNull String permissionDes,PermissionCallback runnable,@ permissions){
if(permissions == null || permissions.length == 0)return;
hewlett packard
// this.permissionrequestCode = requestCode;
this.permissionRunnable = runnable;
tx是什么意思
if((Build.VERSION.SDK_INT < Build.VERSION_CODES.M) || checkPermissionGranted(permissions)){
if(permissionRunnable!=null){
permissionRunnable.hasPermission();
permissionRunnable = null;
}
}el{
//permission has not been granted.
requestPermission(permissionDes,permissionRequestCode,permissions);
hail
}
}
private boolean checkPermissionGranted(String[] permissions){
boolean flag = true;
for(String p:permissions){
if(ActivityCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED){
flag = fal;
break;
}
}
return flag;
}
private void requestPermission(String permissionDes,final int requestCode,final String[] permissions){
if(shouldShowRequestPermissionRationale(permissions)){
// Provide an additional rationale to the ur if the permission was not granted
// and the ur would benefit from additional context for the u of the permission.
// For example, if the request has been denied previously.
// Snackbar.make(getWindow().getDecorView(), requestName,
// Snackbar.LENGTH_INDEFINITE)
// .tAction(_ok, new View.OnClickListener() {
qty// @Override
// public void onClick(View view) {
// questPermissions(BaAppCompatActivity.this,
// permissions,
// requestCode);
// }
// })
// .show();
//如果⽤户之前拒绝过此权限,再提⽰⼀次准备授权相关权限
new AlertDialog.Builder(this)
.tTitle("提⽰")
.tMessage(permissionDes)
.tPositiveButton(_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}).show();
}el{
// Contact permissions have not been granted yet. Request them directly.
}
private boolean shouldShowRequestPermissionRationale(String[] permissions){
boolean flag = fal;
december是什么意思for(String p:permissions){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,p)){
flag = true;
break;
}
}
return flag;
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
预告片英语
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == permissionRequestCode){
if(verifyPermissions(grantResults)){
if(permissionRunnable!=null) {
permissionRunnable.hasPermission();
permissionRunnable = null;
}
}el{
showToast("暂⽆权限执⾏相关操作!");
if(permissionRunnable!=null) {
permissionRunnable = null;
}
}
}el{
}
}
public boolean verifyPermissions(int[] grantResults) {
// At least one result must be checked.
if(grantResults.length < 1){
return fal;
}
// Verify that each required permission has been granted, otherwi return fal.