android6.0及6.0以上的系统在权限管理方面和android6.0以下的系统有了很大的不同,都知道android6.0以下的系统只需要在AndroidManifest.xml进行配置就可以了,而android6.0及6.0以上的系统不仅需要在AndroidManifest.xml进行配置,还需要在使用的时候去动态的申请权限的获取,对于用户来说,他可以赋予这个权限的使用,也可以拒绝这权限的使用,对于android6.0及6.0以上的系统如果不进行动态获取权限,并做相应的处理,在用户使用的时候就会造成闪退;对此先说下android6.0及6.0以上的系统权限上的一些改变吧:
Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。
Normal Permissions如下:
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
Dangerous Permissions:
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
以上是谷歌对android6.0及6.0以上系统的权限分类,做android开发的都知道,国内android手机的碎片化很严重,各个品牌厂商对android系统都做了更改,在开发中接触了华为手机和小米手机,这俩个品牌的手机在处理上会有些不一样,特别是系统拨号权限的处理,对于华为手机来说,并没有将拨号权限列入Dangerous Permissions这类;而小米却将拨号权限列入为小米手机的最高权限,特别是小米6.0.1系统后,小米Max就是如此;
下面说下动态获取权限的流程:
1、 在AndroidManifest文件中添加需要的权限;这个跟之前一样的,没有变化
2、检查权限:
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
}else{
//
}
3、申请授权
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
4、处理权限申请回调
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
是不是感觉挺麻烦的,如果需要申请的地方比较多的时候,特别麻烦;本人在开始的时候也是如此;所有就对其做了一些小小的封装,将其封装在BaseActivity、BaseFragmentActivity、BaseFragment中,继承这些类的子类只需要重写父类的方法就可以,具体如下:
BaseActivity:
/**
* 权限回调Handler
*/
private PermissionHandler mHandler;
/**
* 请求权限
*
* @param permissions
* 权限列表
* @param handler
* 回调
*/
protected void requestPermission(String[] permissions, PermissionHandler handler) {
if (PermissionUtils.hasSelfPermissions(this, permissions)) {
handler.onGranted();
} else {
mHandler = handler;
ActivityCompat.requestPermissions(this, permissions, 001);
}
}
/**
* 权限请求结果
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (mHandler == null) {
return;
}
if (PermissionUtils.verifyPermissions(grantResults)) {
mHandler.onGranted();
} else {
if (!PermissionUtils.shouldShowRequestPermissionRationale(this, permissions)) {
if (!mHandler.onNeverAsk()) {
Toast.makeText(BaseActivity.this, "权限已被拒绝,请在设置-应用-权限中打开", Toast.LENGTH_LONG).show();
}
} else {
mHandler.onDenied();
}
}
}
/**
* 权限回调接口
*/
public abstract class PermissionHandler {
/**
* 权限通过
*/
public abstract void onGranted();
/**
* 权限拒绝
*/
public void onDenied() {
}
/**
* 不再询问
*
* @return 如果要覆盖原有提示则返回true
*/
public boolean onNeverAsk() {
return false;
}
}
MainActivity:
public String[] CALL =new String[]{Manifest.permission.CALL_PHONE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(CALL, new PermissionHandler() {
@Override
public void onGranted() {
//允许拨打电话动作
}
@Override
public void onDenied() {
super.onDenied();
//拒绝拨打电话动作
}
});
}
});
}
@Override
protected void onResume() {
super.onResume();
}
这样子就实现了,是不是要方便很多,效果如下: