Android permissions - Manifest or android.manifest

2019-07-14 05:28发布

问题:

I begin development with Android. I use SDK 23 with Android M permissions's system. On internet'examples, when I want to checkPermission, I found :

this.checkSelfPermission(Manifest.permission.READ_CONTACTS)

But It doesn't work for me. I need to write :

this.checkSelfPermission(android.manifest.permission.READ_CONTACTS)

I would like to know the difference and why it doesn't work.

If it could help, this is my Manifest file (only permissions):

<uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

Thank you

回答1:

When you are asking for if permission is allowed you should use android.Manifest.permission.XX and then for asking you should use Manifest.permission.XX Here is an example:

 public static boolean checkCameraPermissions(Activity activity, int permission) {
    if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.CAMERA}, permission);
        return false;
    }
    return true;
}