联系人选择器不适用于 Android 11

contact picker doesn't work on Android 11

我有一个问题,联系人选择器代码在任何低于 11 的 android 版本上工作正常,但是当我在 android 11 上测试它时,我有这个错误:

2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:514)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:138)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:52)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.CursorWrapper.getString(CursorWrapper.java:141)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at com.tortohub.client.add.AddClientActivity.onActivityResult(AddClientActivity.java:159)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:8310)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.os.Looper.loop(Looper.java:223)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我使用来自另一个 Whosebug 问题的联系人选择器代码:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == RESULT_PICK_CONTACT) {
                Cursor cursor;
                try {
                    String phoneNo = "";
                    String name = "";
                    Log.i("intent",data.getData().toString());
                    Uri uri = data.getData();
                    cursor = this.getContentResolver().query(uri, null, null, null, null);
                    cursor.moveToFirst();

                    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    phoneNo = cursor.getString(phoneIndex);

                    int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    name = cursor.getString(nameColumnIndex);

                    cursor.close();
                    Log.i("phone", phoneNo);
                    Log.i("name", name);
                    contactPicked(name,phoneNo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.e("MainActivity", "Failed to pick contact");
        }
    }
    private void pickContact(){
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
    }

我还注意到 android 11 内容 uri 略有变化:

android 11:意图:内容://com.android.contacts/data/3
android 10:意图:内容://com.android.contacts/data/1

我解决了问题:原因是在 android 11 中我不得不手动请求联系人读取权限,而在 android 10 中该代码工作正常。
解决方案:手动请求联系人读取权限

检查下面link in android 11 你必须添加权限

 <uses-permission android:name="android.permission.READ_PHONE_STATE"
                 android:maxSdkVersion="29" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />

https://developer.android.com/about/versions/11/privacy/permissions