簡體   English   中英

如何從安卓手機中獲取已刪除的聯系人以及如何恢復它

[英]how to get the deleted contact from the android phone and how to restore it

我正在這樣做,但這並沒有為我獲取電話號碼。 我正在使用聯系提供程序,我想從我的手機中獲取已刪除的聯系人並將其恢復到我的手機中。 提前致謝。

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

只需在下面的代碼中傳遞您要訪問的 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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM