判断本程序是否拥有某权限的方法:
- private static final String EXTERNAL_STORAGE_PERMISSION = "android.permission.WRITE_EXTERNAL_STORAGE";
- private static boolean hasExternalStoragePermission(Context context) {
- int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
- return perm == PackageManager.PERMISSION_GRANTED;
- }
判断某个程序是否拥有某权限的方法:
- private static boolean checkPermission(Context context, String permName, String pkgName){
- PackageManager pm = context.getPackageManager();
- if(PackageManager.PERMISSION_GRANTED == pm.checkPermission(permName, pkgName)){
- System.out.println(pkgName + "has permission : " + permName);
- return true;
- }else{
- //PackageManager.PERMISSION_DENIED == pm.checkPermission(permName, pkgName)
- System.out.println(pkgName + "not has permission : " + permName);
- return false;
- }
- }