0

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??

XXX
  • 8,996
  • 7
  • 44
  • 53
siva
  • 167
  • 1
  • 12
  • What's happening when you try to delete the contact? Do you get any errors, etc.? – Aleks G Mar 12 '12 at 17:13
  • It is in the mobile na, i could not see any errors.it is saying "contact deleted". but it is actually not deleting the contact. – siva Mar 12 '12 at 17:17
  • What Android versions do you use? On device? And in the Emulator? – PKeidel Mar 12 '12 at 17:26
  • please look at my edited post... my mobile has V2.3 and emulator has V2.2 – siva Mar 12 '12 at 17:27
  • i have tried this link http://stackoverflow.com/questions/7351444/deleting-raw-contacts-in-android-2-x/8692909#8692909, still no use, working in the emulator but not on the device.. – siva Mar 12 '12 at 18:38

1 Answers1

0

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.

marmor
  • 27,641
  • 11
  • 107
  • 150