Monday, August 9, 2010

Delete Last Call from CallLog Programatically in android

 AIM: To delete the last call from the CallLog Programatically
SOLUTION:
Step 1) Create a Method as follows:

public boolean DeleteLastCall(Context context) {
boolean isDeleted = false;
// Load the calls Log data via context calls Content resolver
android.database.Cursor c = context.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC");
ArrayList CallRecordsList = new ArrayList();
// Retrieve the column-indixes of phoneNumber, date and calltype
Cursor cursor = c;
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
CallRecord CallRecords = LoadCallRecord(cursor); // Load attributes of last call
CallRecordsList.add(CallRecords);
cursor.moveToNext();
}
}
if (CallRecordsList.size() > 0) {

int deletedRows = context.getContentResolver().delete(
android.provider.CallLog.Calls.CONTENT_URI,
"_ID=" + CallRecordsList.get(0).row_id, null);
if (deletedRows > 0)
isDeleted = true;
else
isDeleted = false;
// isFirstRecord=1;
Log.i("Row Deleted status:", " " + isDeleted);

}
return isDeleted;
}

private CallRecord LoadCallRecord(Cursor cursor) {
CallRecord contactData = new CallRecord();
String[] ColumnNames = cursor.getColumnNames();
for (int intLoop = 0; intLoop < ColumnNames.length; intLoop++) {
// Load id of Last call
if (android.provider.CallLog.Calls._ID
.compareTo(ColumnNames[intLoop]) == 0)
contactData.row_id = cursor.getString(intLoop);
}

return contactData;
}
// Attributes of callRecord
class CallRecord {
public String row_id;

}


Step 2) Call the above method as 
                  DeleteLastCall(this);

By this way we can delete the last call from the callLog programatically.


2 comments:

  1. i tried using ur tutorial to delete - Last OutGoing call -- on OFFHOOK and/or IDLE state

    but its giving an error for ".row_id" for following line ->

    int deletedRows = context.getContentResolver().delete(
    android.provider.CallLog.Calls.CONTENT_URI,
    "_ID=" + CallRecordsList.get(0).row_id, null);




    Also, I am unable to detect the phone number of outgoing call on OFFHOOK or IDLE state - any clue - how to do this?


    any help is appreciated.

    ReplyDelete
  2. This code is not working on Android Cell , can you sugget it why is it so ?

    ReplyDelete

Android Developers Blog

Ram's shared items