Search code examples
androidcursorcalllog

delete from callLog - Android 2.3.3


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.


Solution

  • 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)