0

I'm trying to write a user input to contacts in android. I already define uses-permission for this, but the program shows exception.

First this is my manifest....

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.providertest02" >

        <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
        <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
....
    </manifest>

Then this is my code...

        String name = nameEdit.getText().toString();
        String phone = phoneEdit.getText().toString();
        values.put(ContactsContract.Contacts.DISPLAY_NAME, name);
        values.put(ContactsContract.Contacts.STARRED, 1);

        Uri uri = getContentResolver().insert(ContactsContract.Contacts.CONTENT_URI, values);

The error message is

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{7393c6f 12376:com.example.providertest02/u0a77} (pid=12376, uid=10077) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

myoldgrandpa
  • 871
  • 7
  • 21
  • What is your `targetSdkVersion` (e.g., in your `app/build.gradle` file in Android Studio), and what version of Android are you testing on? – CommonsWare Oct 07 '15 at 14:20
  • targetSdkVersion is 23 and my test emulated device is also API 23. – myoldgrandpa Oct 07 '15 at 14:27
  • `READ_CONTACTS` and `WRITE_CONTACTS` are `dangerous` permissions. With a `targetSdkVersion` of 23 or higher, and testing on Android 6.0 or higher, you have more work to do. This has been discussed before; please see [this question](http://stackoverflow.com/questions/32635704/cant-get-the-send-sms-permission). – CommonsWare Oct 07 '15 at 14:29

0 Answers0