Android Question Delete contact SIMCARD

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
I have this code:

B4X:
'Cancella
'Deletes the contact with the given name.
Sub DeleteContactByName(Name As String)
'***********This Delete number to Contact  and work 
   Dim u As Uri
   u.Parse("content://com.android.contacts/data")
   Dim crsr As Cursor = cr.Query(u, Array As String("_id", "raw_contact_id"), _
      "mimetype = ? AND data1 = ?", Array As String("vnd.android.cursor.item/name", Name), "")
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      Dim rawId As Long = crsr.GetLong("raw_contact_id")
      Log(cr.Delete(u, "raw_contact_id = ?", Array As String(rawId)))
      Dim u2 As Uri
      u2.Parse("content://com.android.contacts/raw_contacts")
      Log(cr.Delete(u2, "_id = ?", Array As String(rawId)))
   Next
   crsr.Close
'******** End Delete number to contact
  
'*********** This Delete number to Sim
   Dim simUri As Uri
   simUri.Parse("content://icc/adn")
   Dim crsr1 As Cursor = cr.Query(simUri, Array As String("_id","name","number"), "name = ?", Array As String(Name), "")
   For i = 0 To crsr1.RowCount - 1
      crsr1.Position = i
      Dim rawId1 As Long = crsr1.GetLong("_id")
           Log(cr.Delete(simUri, "_id = ?", Array As String(rawId1))) 
      Next
   crsr1.Close
'********** End Delete number to Sim
 'Rileggo i dati
   ListView2.Clear
   ReadSimContacts
End Sub



Now this code works ( '***********This Delete number to Contact and work ) and delete numbers from phone contacts, in each case in the memory of the sim are still present. Then use the second piece of code ('*********** This Delete number to Sim) donw work and also:
1) also i have "where" in the query with the name, returned 3 records (all existing contacts on the sim)
2) Do not perform any data deletion

Any idea or solution ??
Thanik you
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
I have this code:

B4X:
'Cancella
'Deletes the contact with the given name.
Sub DeleteContactByName(Name As String)
'***********This Delete number to Contact  and work
   Dim u As Uri
   u.Parse("content://com.android.contacts/data")
   Dim crsr As Cursor = cr.Query(u, Array As String("_id", "raw_contact_id"), _
      "mimetype = ? AND data1 = ?", Array As String("vnd.android.cursor.item/name", Name), "")
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      Dim rawId As Long = crsr.GetLong("raw_contact_id")
      Log(cr.Delete(u, "raw_contact_id = ?", Array As String(rawId)))
      Dim u2 As Uri
      u2.Parse("content://com.android.contacts/raw_contacts")
      Log(cr.Delete(u2, "_id = ?", Array As String(rawId)))
   Next
   crsr.Close
'******** End Delete number to contact

'*********** This Delete number to Sim
   Dim simUri As Uri
   simUri.Parse("content://icc/adn")
   Dim crsr1 As Cursor = cr.Query(simUri, Array As String("_id","name","number"), "name = ?", Array As String(Name), "")
   For i = 0 To crsr1.RowCount - 1
      crsr1.Position = i
      Dim rawId1 As Long = crsr1.GetLong("_id")
           Log(cr.Delete(simUri, "_id = ?", Array As String(rawId1)))
      Next
   crsr1.Close
'********** End Delete number to Sim
'Rileggo i dati
   ListView2.Clear
   ReadSimContacts
End Sub



Now this code works ( '***********This Delete number to Contact and work ) and delete numbers from phone contacts, in each case in the memory of the sim are still present. Then use the second piece of code ('*********** This Delete number to Sim) donw work and also:
1) also i have "where" in the query with the name, returned 3 records (all existing contacts on the sim)
2) Do not perform any data deletion

Any idea or solution ??
Thanik you
Marco

I found also this documentation:

https://simsalabim.googlecode.com/svn-history/r7/trunk/src/at/fhj/swd07/simsalabim/SimUtil.java

seem that Resolver have bug ( // TODO: currently this always returns ALL contacts on the SIM. Is this a bug in the content provider? ):

B4X:
    public int deleteContact(Contact contact) {
        // check, that only one contact with this identifiers existing
        // TODO: currently this always returns ALL contacts on the SIM. Is this a bug in the content provider?
//        Cursor results = resolver.query(simUri, new String[]{android.provider.BaseColumns._ID}, "tag='"+contact.name+"' AND number='"+contact.number+"'", null, null);
//        int rowCount = results.getCount();
//        if (rowCount > 1) {
//            return rowCount;
//        }
      
        // TODO: Can this ever return >1 after check above?
        int deleteCount = resolver.delete(simUri, "tag='"+contact.name+"' AND number='"+contact.number+"'", null);
        if(deleteCount != 1) {
            return -1;
        }
      
        return 0;
    }
 
Upvote 0
Top