0

I have this code to delete a Callfrom CallLog.Callsprovider :

            String strNumber= "myPhoneNumber"

            String queryString= "NUMBER='" + strNumber + "'";

            Log.v("Number", queryString); 

            int i=context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, queryString, null); 
            if(i<1) 

            {  
               // do my stuff
            } 

This code work on android 2.2 but it does'nt work on android 2.3.3

There'is no error in the LogCat.

113408
  • 3,364
  • 6
  • 27
  • 54

2 Answers2

1

You should use the contract class provided by the SDK ( CallLog.Calls in your situation ) when building your queries.

The contract classes are meant to map the object model to the corresponding data names in the database. When the android team need to update a column name in the database, they also update the contract class and then no change is required for developpers.

Then try to change you query string to :

String queryString = CallLog.Calls.NUMBER + "='" + strNumber + "'";

NOTE: the single-quote marks are necessary.

Also make sure the number is exactly matching the database number.

( +33678541236 != 0678541236 )

To be sure to delete the correct row you can do the following :

Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumber , "");
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);

(didn't try it but should work with small corrections)

zen_of_kermit
  • 1,404
  • 2
  • 13
  • 19
Timothée Jeannin
  • 9,652
  • 2
  • 56
  • 65
  • My mistake was myPhoneNumber doesn't much the `NUMBER`in the database . like in your example. Thanks for the help ! – 113408 Jun 04 '12 at 14:07
0
private void deleteNumber() {
            try {
                String strNumberOne[] = { "00577698160" };
                Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumberOne, "");
                boolean bol = cursor.moveToFirst();
                if (bol) {
                    do {
                        int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
                        getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);
                    } while (cursor.moveToNext());
                }
            } catch (Exception ex) {
                System.out.print("Exception here ");
            }
        }
aftab
  • 1,141
  • 8
  • 21
  • 40