【问题标题】:how to get the deleted contact from the android phone and how to restore it如何从 android 手机中获取已删除的联系人以及如何恢复它
【发布时间】:2021-01-06 03:10:41
【问题描述】:

我正在这样做,但这并没有为我获取电话号码。我正在使用联系人提供商,我想从我的手机中获取已删除的联系人并将其恢复到我的手机。 提前致谢。

public static final String WHERE_MODIFIED1 = "( "+ ContactsContract.RawContacts.DELETED + "=1)";
public void readContacts() {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query((ContactsContract.RawContacts.CONTENT_URI),
            null, WHERE_MODIFIED1, null, null);

    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);
                while (pCur.moveToNext()) {
                    phone = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    System.out.println("phone" + phone);
                }
                pCur.close();
            //}
            if (phone != null) {
                contactList.add(new Contact(name, phone));
            }
            Log.e("hvy", "onCreaterrView  Phone Number: name = " + name
                    + " No = " + phone);
        }
    }
    cur.close();
}

如果我运行这段代码,我的日志会显示这个

E/hvy: onCreaterrView  Phone Number: name = Abc No = null
I/System.out: name : Abc, ID : 4090
E/hvy: onCreaterrView  Phone Number: name = Abc No = null
I/System.out: name : Abc, ID : 4091
E/hvy: onCreaterrView  Phone Number: name = Abc No = null
I/System.out: name : Gagaga, ID : 4092
E/hvy: onCreaterrView  Phone Number: name = Gagaga No = null
I/System.out: name : Shah, ID : 4093
E/hvy: onCreaterrView  Phone Number: name = Shah No = null
I/System.out: name : CardRecharge, ID : 4105

【问题讨论】:

    标签: android android-studio android-contentprovider android-contacts


    【解决方案1】:

    只需在下面的代码中传递您要获取的 id

     ArrayList arrayList = new ArrayList();
        arrayList.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter("caller_is_syncadapter", "true").build()).withSelection("_id=?", new String[]{String.valueOf(id)}).withValue("deleted", 0).withYieldAllowed(true).build());
        try {
            YourActivity.this.getContentResolver().applyBatch("com.android.contacts", arrayList);
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多