Android 6.0权限详解及封装
Android Studio 2.2.2
sdk 25.0.1
GitHub代码地址
前几天看了郭神对Android 6.0 之后的权限讲解,今天把它整理了一些写了个demo 分享给大家。
注意点:
1、如果在build.gradle 中设置targetSdkVersion 为21 表示兼容到5.0的版本,则在程序运行时不需要进行权限的判断,因为只有在6.0以后才需要对运行时的权限进行判断。
2、如果程序是从低版本升级上去,那么之前版本升级的权限默认有效。
3、要申请的所有权限必须在manifest中先注册
checkSelfPermission
首先这个方法只有在activity中才可以调用,用来判断权限是否申请。如果已经申请了则返回PackageManager.PERMISSION_GRANTED 否则返回PackageManager.PERMISSION_DENIED.
doucumentation
public static int checkSelfPermission(@android.support.annotation.NonNull @NotNull android.content.Context context,@android.support.annotation.NonNull @NotNull java.lang.String permission)
Determine whether you have been granted a particular permission.
Parameters:
permission - The name of the permission being checked.
Returns:
android.content.pm.PackageManager.PERMISSION_GRANTED if you have the permission,
or android.content.pm.PackageManager.PERMISSION_DENIED if not.
requestPermissions
请求权限方法,传值要求 当前的Activity ,String[] 权限数组,requestCode (与startActivityForResult()中的requestCode一样,在回调的时候会传过去,onRequestPermissionsResult(),对应不同requestCode 的权限申请结果)
documentation
public static void requestPermissions(@android.support.annotation.NonNull android.app.Activity activity,
@android.support.annotation.NonNull java.lang.String[] permissions,
int requestCode)
//多权限申请
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.CALL_PHONE);
}
if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()){
ActivityCompat.requestPermissions(this,permissionList.toArray(new String[permissionList.size()]),1);
} else{
doSomething();
}
//单个权限申请
// if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
// ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},1);
// }else{
// do something
// }
onRequestPermissionsResult
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
申请权限的回调方法 requestCode就是之前调用的时候传的值,String[] permissions 申请的所有权限,int[] grantResults 权限申请结果数组。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 1:
if (grantResults.length > 0){
for (int grantResult : grantResults){
if (grantResult != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "某个权限被拒绝了", Toast.LENGTH_SHORT).show();
return;
}
}
doSomething();
}
// 单个权限申请回调
// if (grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
// makeCall();
// }else {
// Toast.makeText(this, "权限被拒绝了", Toast.LENGTH_SHORT).show();
// }
break;
default:
break;
}
}
封装
为了方便权限申请,简化代码,下面对权限的申请进行一个封装。
直接看代码吧:
//定义一个接口
public interface PermissionListener {
void onGranted();
void onDenied(List<String> deniedPermissions);
}
//定义一个Activity收集类
public class ActivityCollector {
private static List<Activity> activityList = new ArrayList<>();
//在activity 的onCreate 中执行此方法
public static void addActivity(Activity activity) {
activityList.add(activity);
}
//在activity 的onDestroy 中执行此方法
public static void removeActivity(Activity activity) {
activityList.remove(activity);
}
//获取当前的Activity
public static Activity getTopActivity() {
if (activityList.isEmpty()) {
return null;
} else {
return activityList.get(activityList.size() - 1);
}
}
}
//创建BaseActivity 里面做一些权限处理
public class BaseActivity extends AppCompatActivity {
private static PermissionListener mListener;
//创建静态方法,这样可以在没有activity实例的类中调用该方法,只要加个类名就可以了
public static void requestRuntimePermission(String[] permissions, PermissionListener listener){
//获取当前的activity 如果没有就返回
Activity topActivity = ActivityCollector.getTopActivity();
if (topActivity == null) {
return;
}
mListener = listener;
List<String> permissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(topActivity,permission) != PackageManager.PERMISSION_GRANTED){
permissionList.add(permission);
}
}
if (!permissionList.isEmpty()){
ActivityCompat.requestPermissions(topActivity,permissionList.toArray(new String[permissionList.size()]),1);
} else{
mListener.onGranted();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 1:
if (grantResults.length > 0){
List<String> deniedPermissions = new ArrayList<>();
for (int i = 0; i < grantResults.length; i++) {
int grantResult = grantResults[i];
String permission = permissions[i];
if (grantResult != PackageManager.PERMISSION_GRANTED){
deniedPermissions.add(permission);
}
}
// do something
if (deniedPermissions.isEmpty()){
mListener.onGranted();
}else{
mListener.onDenied(deniedPermissions);
}
}
break;
default:
break;
}
}
}
//在Activity中调用
public class PermissionActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
//
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//
ActivityCollector.removeActivity(this);
}
public void permissionsOnClick(View v){
requestRuntimePermission(new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA}, new PermissionListener() {
@Override
public void onGranted() {
Toast.makeText(PermissionActivity.this, "所有权限已同意", Toast.LENGTH_SHORT).show();
}
@Override
public void onDenied(List<String> deniedPermissions) {
for (String deniedPermission : deniedPermissions) {
Toast.makeText(PermissionActivity.this, "未同意的权限"+deniedPermission, Toast.LENGTH_SHORT).show();
}
}
});
}
}
关注微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。