2015-09-28 75 views
1

這是我的java代碼。如何測試Android M上的ActivityCompat.requestPermission?

public static void checkSinglePermissionCompat(final Activity activity, 
               ActionNeedsPermission action, final String permission, 
               final int permissionCode, String message) { 
    int hasPermission = ActivityCompat.checkSelfPermission(activity, permission); 
    if (hasPermission == PackageManager.PERMISSION_GRANTED) { 
     action.performAction(); 
    } else { 
     if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { 
      showRationaleDialog(activity, message, 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, 
              int which) { 
          ActivityCompat.requestPermissions(activity, new String[]{permission}, permissionCode); 
         } 
        }); 
     } else { 
      ActivityCompat.requestPermissions(activity, new String[]{permission}, permissionCode); 
     } 
    } 
} 

我gradle這個文件

android { 
compileSdkVersion "android-MNC" 
buildToolsVersion "23.0.1" 

defaultConfig { 
    minSdkVersion "MNC" 
    targetSdkVersion "MNC" 
} 
} 

和我的清單文件

<uses-permission-sdk-m android:name="android.permission.CAMERA"/> 
<uses-permission-sdk-m android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

<uses-permission-sdk-m android:name="android.permission.RECORD_AUDIO" /> 
<uses-permission-sdk-m android:name="android.permission.SEND_SMS" /> 

它只是回調拒絕的權限,並沒有表現出該系統的權限請求對話框。任何人都可以幫助我? THX

回答

0

請參考此樣本Google sample code

<!-- Note that all required permissions are declared here in the Android manifest. 
On Android M and above, use of these permissions is only requested at run time. --> 
**<uses-permission android:name="android.permission.CAMERA"/>** 

<!-- The following permissions are only requested if the device is on M or above. 
On older platforms these permissions are not requested and will not be available. --> 
**<uses-permission-sdk-m android:name="android.permission.READ_CONTACTS" /> 
<uses-permission-sdk-m android:name="android.permission.WRITE_CONTACTS" />**