首页 > 解决方案 > 如何以编程方式在android 10中恢复已删除的联系人

问题描述

我想创建一个可以恢复已删除联系人的应用程序。它适用于所有其他设备,但在 android 10 中,应用程序无法获取已删除的联系人。是否有任何解决方案可以从 android 10 中获取已删除的联系人。

这是我的代码,可以恢复已删除的联系人

public static final String WHERE_MODIFIED1 = "( " + ContactsContract.RawContacts.DELETED + "=1)";
ContentResolver cr = getContentResolver();
    Cursor cur = cr.query((ContactsContract.RawContacts.CONTENT_URI),
            null, WHERE_MODIFIED1, null, (DeletedContactRecovery.hasHoneycomb() ? "sort_key" : "display_name") + " COLLATE LOCALIZED ASC");

    assert cur != null;
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String phone = null;
            //if (!(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)) {
            System.out.println("name : " + name + ", ID : " + id);

            // get the phone number
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            assert pCur != null;
            while (pCur.moveToNext()) {
                phone = pCur.getString(
                        pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                System.out.println("phone" + phone);
            }
            pCur.close();
            //}
            if (id != null) {
                contactList.add(new Contact(name, phone, id));
            }
            Log.e("hvy", "onCreaterrView  Phone Number: name = " + name
                    + " No = " + phone);
        }
    }
    cur.close();

标签: androidandroid-studiocontactsandroid-contacts

解决方案


对于要阻止或恢复联系人的 android 应用程序,有必要将其设为默认 Dailer 或 SMS 应用程序。当我厌倦阅读 android 10 中的阻止联系人时,我曾经遇到过同样的问题,即使我的应用程序在 android 10 下运行良好。我不知道这里的解决方案到底是什么,但尝试将应用程序设为默认值,然后尝试阅读联系人。


推荐阅读