Delete call from call log after call end

18,552

Solution 1

First you have to set up a broadcast receiver to detect the phone state. Here's the exact same question: Stackoverflow - Intent to be fired when a call ends?

And now for deleting the call log entry, here is the first link on google: Call log deletion in Android
In the example he deletes all call entry for a specific number, but you can change the query to delete the entry for a specific call log id.

Hope this helps. Cheers

Solution 2

Im using 4.2.2 anyway i had to modify the aftab's code as it was not working for me. It could be a asych issue giving what i was trying to do is update the call log right after an incoming call is ended. I think i have to give O/S enough time to update the table before i delete the entry or it wont exist :

private void deleteNumber(String phoneNumber) {

    try {
        Thread.sleep(4000);
        String strNumberOne[] = { phoneNumber };
        Cursor cursor = context.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));
                context.getContentResolver().delete(
                        CallLog.Calls.CONTENT_URI,
                        CallLog.Calls._ID + "= ? ",
                        new String[] { String.valueOf(idOfRowToDelete) });
            } while (cursor.moveToNext());
        }
    } catch (Exception ex) {
        Log.v("deleteNumber",
                "Exception, unable to remove # from call log: "
                        + ex.toString());
    }
}

and to call the function i run on another thread (since im sleeping) :

  new Thread() {
 public void run() {
deleteNumber(incomingNumber);
    }
}.start();

after adding the sleep it seems to work when trying to delete right after a call is ended.

UPDATE: after last comment realized we can set up a contentobserver on the android provider call log uri:

public class BlockerContentObserver extends ContentObserver{

private Context context;
private String phoneNumber;


public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public BlockerContentObserver(Handler handler,Context context) {
    super(handler);
this.context=context;
}

 @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

@Override
public void onChange(boolean selfChange) {
    // TODO Auto-generated method stub
    super.onChange(selfChange);
    Log.v(Consts.TAG,"has call log changed:"+selfChange);
    deleteNumber(phoneNumber);

}   

private void deleteNumber(String phoneNumber) {


    try {

        String strNumberOne[] = { phoneNumber };
        Cursor cursor = context.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));
                context.getContentResolver().delete(
                        CallLog.Calls.CONTENT_URI,
                        CallLog.Calls._ID + "= ? ",
                        new String[] { String.valueOf(idOfRowToDelete) });
            } while (cursor.moveToNext());
        }
    } catch (Exception ex) {
        Log.v(Consts.TAG,
                "Exception, unable to remove # from call log: "
                        + ex.toString());
    }
}

}

Now we register to listen to changes in the call log DB using this:

mContentObserver = new BlockerContentObserver(new Handler(), context);

then we make a method to either register for events or unreigster:

/*handles the registration of our content observer used for monitoring the call log*/
private void RegisterContentObserver(boolean shouldRegister){
    if(shouldRegister)
    {

        context.getContentResolver().registerContentObserver(
                android.provider.CallLog.Calls.CONTENT_URI,
                true,
                mContentObserver);

    }
else {

    try {  
        context.getContentResolver().unregisterContentObserver(mContentObserver);  
    } catch (IllegalStateException ise) {  
        // Do Nothing.  Observer has already been unregistered.  
    }  

}
    }

Solution 3

I just try this method its working great on my HTC with 4.0.3

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 ");
            }
        }

Solution 4

modified version of earlier answsers. you don't really need a while loop. Also you don't need Thread.sleep(4000), register a ContentObserver for CallLog.Calls.CONTENT_URI and call the following method in onChange. but just before calling that make sure you unregister that ContentObserver

public static void deleteLastCallLog(Context context, String phoneNumber) {

        try {
            //Thread.sleep(4000);
            String strNumberOne[] = { phoneNumber };
            Cursor cursor = context.getContentResolver().query(
                    CallLog.Calls.CONTENT_URI, null,
                    CallLog.Calls.NUMBER + " = ? ", strNumberOne, CallLog.Calls.DATE + " DESC");

            if (cursor.moveToFirst()) {
                    int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));                      
                    int foo = context.getContentResolver().delete(
                            CallLog.Calls.CONTENT_URI,
                            CallLog.Calls._ID + " = ? ",
                            new String[] { String.valueOf(idOfRowToDelete) });

            }
        } catch (Exception ex) {
            Log.v("deleteNumber",
                    "Exception, unable to remove # from call log: "
                            + ex.toString());
        }
    }
Share:
18,552

Related videos on Youtube

Shivalinga
Author by

Shivalinga

Updated on May 25, 2022

Comments

  • Shivalinga
    Shivalinga almost 2 years

    I am new to Android development. I want to make a call to a number but I don't want to store the number in my Call log. How can I delete the number from call log after the call ends?

    • clauziere
      clauziere over 11 years
      Please choose your answer.
  • j2emanue
    j2emanue over 10 years
    excellent idea regarding content observer, i'll try to upvote you and update my comment as well to help others.
  • osrl
    osrl over 8 years
    Why do you need a cursor to loop. ContentResolver.delete() can remove multiple records, am I wrong? getContentResolver().delete(CallLog.Calls.CONTENT_URI, CallLog.Calls.NUMBER +"=?", new String[]{phoneNumber}); should do fine
  • Muhammad Umar
    Muhammad Umar over 7 years
    its not working, any idea? it does call the delete function, the bool is true, but the number is in log history as is
  • j2emanue
    j2emanue over 7 years
    did you set up a contentObserver ? are you getting a event sent to you when the call log changes ?