开发者

how to delete all contacts in contact list on android mobile programatically

I want to delete all the contact on one button press from my application so can any one nice person can told me how to delete all the contacts fro开发者_如何学Cm android mobile programmatically on only one button press? answer would be greatly appreciated...

Actually I was surfing from a couple of hours but not got any appropriate answer. That's why I need to post my problem in this nice forum...thanks to such forum....


It is very simple, This code will remove all your contacts.

ContentResolver contentResolver = <your app context>.getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
            contentResolver.delete(uri, null, null);
        }

Done.


Specify READ_CONTACTS and WRITE_CONTACTS permissions in your AndroidManifest.xml.

Iterate through each contact and delete each record: Content Providers

Contacts

Be careful with deleting Contacts! Deleting an aggregate contact deletes all constituent raw contacts. The corresponding sync adapters will notice the deletions of their respective raw contacts and remove them from their back end storage.

With regards to permissions, see Permission groups:

Caution: Future versions of the Android SDK might move a particular permission from one group to another. Therefore, don't base your app's logic on the structure of these permission groups.

For example, READ_CONTACTS is in the same permission group as WRITE_CONTACTS as of Android 8.1 (API level 27). If your app requests the READ_CONTACTS permission, and then requests the WRITE_CONTACTS permission, don't assume that the system can automatically grant the WRITE_CONTACTS permission.

Note: Your app still needs to explicitly request every permission it needs, even if the user has already granted another permission in the same group. In addition, the grouping of permissions into groups may change in future Android releases. Your code shouldn't have logic that depends on a set of particular permissions being in the same group.


The following snippet will help you delete all contacts, not only from the contacts table but also from the data and raw_contacts tables.

private void deleteAllContacts(){
        ContentResolver contentResolver = getContentResolver();
    
        Uri rawUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    
        ops.add( ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).
                withSelection(ContactsContract.RawContacts._ID + ">? "
                        ,new String[]{ "-1" }).build()); //sets deleted flag to 1
    
        ops.add(ContentProviderOperation.newDelete(rawUri).
                withSelection(ContactsContract.RawContacts._ID + ">? "
                        ,new String[]{ "-1" }).build()); //erases
    
        try {
            contentResolver.applyBatch( ContactsContract.AUTHORITY, ops );
        } catch ( RemoteException e) {
            Log.d( "ContactsActivity", "RemoteException --> " + e );
            e.printStackTrace();
        } catch ( OperationApplicationException e) {
            Log.d( "ContactsActivity", "OperationApplicationException --> " + e );
            e.printStackTrace();
        }
    }

Enjoy!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜