详解Android 检测权限的三种写法
本文介绍了详解Android检测权限的三种写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
权限检测生效条件:
- targetSdkVersion以及compileSdkVersion升级到23及以上
- 运行Android系统6.0及以上
三种检测权限写法:
publicstaticbooleancheckPermission1(Contextcontext,String[]permissions){
PackageManagerpackageManager=context.getPackageManager();
StringpackageName=context.getPackageName();
for(Stringpermission:permissions){
intper=packageManager.checkPermission(permission,packageName);
if(PackageManager.PERMISSION_DENIED==per){
Log.w(TAG,"requiredpermissionnotgranted.permission="+permission);
returnfalse;
}
}
returntrue;
}
publicstaticbooleancheckPermission2(Contextcontext,String[]permissions){
for(Stringpermission:permissions){
intper=context.checkPermission(permission,Process.myPid(),Process.myUid());
if(PackageManager.PERMISSION_GRANTED!=per){
Log.w(TAG,"requiredpermissionnotgranted.permission="+permission);
returnfalse;
}
}
returntrue;
}
publicstaticbooleancheckPermission3(Contextcontext,String[]permissions){
for(Stringpermission:permissions){
intper=ContextCompat.checkSelfPermission(context,Manifest.permission.CAMERA);
if(PackageManager.PERMISSION_GRANTED!=per){
Log.w(TAG,"requiredpermissionnotgranted.permission="+permission);
returnfalse;
}
}
returntrue;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。