Delete call from call log after call end
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. Ho开发者_StackOverflow中文版w can I delete the number from call log after the call ends?
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
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.
}
}
}
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 ");
}
}
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());
}
}
精彩评论