Reputation: 167
i have written an application to delete contact from contact list in android. it is working fine in the emulator, but it is not deleting the contact in the device. what could be the problem? i am posting my code.please help me.
ArrayList ops = new ArrayList();
String[] args = new String[] {contactId};
// if id is raw contact id
ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts._ID + "=?", args) .build());
// if id is contact id
//ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts.CONTACT_ID + "=?", args) .build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The finding contact is done using this code
String get_Number(String name) {
String number = null;
String where= "DISPLAY_NAME like ?";
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, where, new String[]{name}, null);
people.moveToFirst();
try{
contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID)); //this is the ID used to delete contact
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// mConno.add(position,phoneNumber);
}
phones.close();
}
}
catch(Exception e)
{
}
return number;
}
and the permissions set in the manifest are
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
Just now i have found a weird behaviour of this app i have developed. before this delete contact app, i have developed an "add contact" app.
So what is happening is It can not delete the contacts added by "add contact" application, but deleting the contacts which are added manually.why this strange thing happening.
But it is working fine in the emulator. but not on the mobile.what is going wrong??
Upvotes: 0
Views: 707
Reputation: 28199
You are trying to delete a raw contact using a contact id. That wouldn't work.
A contact is made up of several raw-contacts, when deleting a contact, all raw-contacts get deleted as well.
Do this:
long contactId = 12345;
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(contactId));
int deleted = getContentResolver().delete(contactUri, null, null);
deleted will be 1 if the operation succeeded.
Upvotes: 0