Android 6.0后强制弹出权限

Android 6.0后一些权限强制必须动态添加,

private static final int REQUEST_EXTERNAL_STORAGE = 1;   // 权限请求码

private static String[] PERMISSIONS_STORAGE = {

        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CALL_PHONE
};


public  void verifyStoragePermissions(Activity activity) {

    // Check if we have read or write permission    

int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);

//android.permission.READ_PHONE_STATE    

int phonePermission=ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE);

if(writePermission!=PackageManager.PERMISSION_GRANTED||readPermission!=PackageManager.PERMISSION_GRANTED||phonePermission!=PackageManager.PERMISSION_GRANTED) {

        // We don't have permission so prompt the user       

 ActivityCompat.requestPermissions(

                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE  
        );
    }

}

虽然是动态请求权限 但是仍然需要在AndroidManifest 注册静态权限

  1. xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="comnii.activity" >  
  4.   
  5.       
  6.     <uses-permission android:name="android.permission.CALL_PHONE"/>  
  7.   
  8.       
  9.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  


你可能感兴趣的:(Android)