简体   繁体   中英

check & update the Phonebook in android

I am trying to update contact in the Phone Book but i need to check whether the contact no is under which head TYPE_WORK , TYPE_MOBILE etc....

 public void updateContact (String newNumber,String oldNumber)
            throws RemoteException, OperationApplicationException {

        //ASSERT: @contactId alreay has a work phone number
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


        String selectPhone = ContactsContract.CommonDataKinds.Phone.NUMBER + "=? AND " + ContactsContract.Data.MIMETYPE + "='"  +
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";

        String[] phoneArgs = new String[]{oldNumber, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};

        if(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(selectPhone, phoneArgs).equals(""))
        {
            Toast.makeText(getContext(), "MOBILE FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
            phoneArgs = new String[]{oldNumber, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
            if(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                    .withSelection(selectPhone, phoneArgs).equals(""))
            {
                Toast.makeText(getContext(), "Work FIELD IS EMPTY", Toast.LENGTH_SHORT).show();

            }
            else
            {
                Toast.makeText(getContext(), "Work FIELD IS NOT EMPTY", Toast.LENGTH_SHORT).show();

                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(selectPhone, phoneArgs)
                        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
                        .build());
            }
        }
        else {
            ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                    .withSelection(selectPhone, phoneArgs)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
                    .build());
        }

        getContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }

I tried Doing But Did not Succeed..PLs Help

You need to retrieve both the contact id and contact type and use it in the update query arguments.I tried this and it worked. -

public void updateContact(String newNumber, String oldNumber) throws RemoteException, OperationApplicationException {

    // get contact type and contact id
    String contactId = null;
    int contactType = -1;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(oldNumber));
    Cursor cursor = getContentResolver().query(
            uri,
            new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.TYPE},
            null,
            null,
            null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
            contactType = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.TYPE));
        }
        cursor.close();
    }

    //ASSERT: @contactId alreay has a work phone number
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


    String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" +
            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";

    String[] phoneArgs = null;

    if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
        Toast.makeText(this, "MOBILE FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
        phoneArgs = new String[]{contactId, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
    } else if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
        Toast.makeText(this, "Work FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
        phoneArgs = new String[]{contactId, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
    }
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(selectPhone, phoneArgs)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
            .build());


    ContentProviderResult[] contentProviderResults = this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    if (contentProviderResults[0].count > 0) {
        Toast.makeText(this, "Updated Contact Successfully", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Contact could not be updated", Toast.LENGTH_SHORT).show();
    }
}

Make sure you have the permission to read and write contacts. Hope this helps !

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