简体   繁体   中英

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

I am doing this but this is not getting the phone number for me. I am usiing the contect provider and I want to get the deleted contact from my phone and restore it to my phone. Thanks in advance.

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();
}

my log showing me this if i run this code

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

just pass the id that you are getting to below code

 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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM