PickContact requires android.permission.READ_CONTA

2019-08-14 21:27发布

问题:

Some info first:

I first created the app in Cordova: see Cordova permissions requires android.permission.READ_CONTACTS, or grantUriPermission()

Now i created the app as a native Android app using Android Studio Builder, it has the same error.

I'm using Android 6.0 ( SDK 23)

According to the docs, ContactPick doesn't need permissions: http://developer.android.com/guide/components/intents-common.html#Contacts

The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.

( but i declared it none the less)

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="me.sapico.notify_arrival">
   <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
   <uses-permission-sdk-23 android:name="android.permission.READ_CONTACTS" />
   <uses-permission-sdk-23 android:name="android.permission.LOCATION_HARDWARE" />
   <uses-permission-sdk-23 android:name="android.permission.INTERNET" />
   <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Here's my code ( it uses a click on ListView)

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position,
                            long arg3)
    {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_SELECT_CONTACT);
        }
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
        String phone = getNumberContacts(data);
        String t = "";
       /* Uri contactUri = data.getData();
        String[] projection = {};//ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // If the cursor returned is valid, get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            //...
        }*/
    }
}

Here's the stacktrace

11-05 01:47:17.008 26632-26688/me.sapico.notify_arrival E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaefbd3e0
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: FATAL EXCEPTION: main
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: Process: me.sapico.notify_arrival, PID: 26632
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/258i49688c038aae982f.3789r2248-292F313F3943312F31533F4545/897 flg=0x1 }} to activity {me.sapico.notify_arrival/me.sapico.notify_arrival.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=26632, uid=10149 requires android.permission.READ_CONTACTS, or grantUriPermission()
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=26632, uid=10149 requires android.permission.READ_CONTACTS, or grantUriPermission()
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:491)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:434)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at me.sapico.notify_arrival.MainActivity.getNumberContacts(MainActivity.java:200)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at me.sapico.notify_arrival.MainActivity.onActivityResult(MainActivity.java:175)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:6428)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-05 01:47:19.260 26632-26632/me.sapico.notify_arrival E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I have had many variants on the permissions / requesting phone information. It mostly fails when i try to get the number.. ( i succeeded once at getting the user name).

Any thoughts? It's also in my Cordova app ( i thought it was Cordova previously), but now it's also in native Android. I know Android 6.0 is recently new, but i can't find anything in the docs / stackoverflow.

回答1:

The answer is somewhere in between. AndroidManifest.xml was declared correctly, but my gradle build settings changed the targetsdk version to 23. Which causes the new permission system to kick in ( see @ http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en )

I changed the gradle build file ( after checking my AndroidManifest on unpacking the apk). And it works now!