一尘不染

通话结束后从通话记录中删除通话

javascript

我是Android开发的新手。我想拨打电话,但不想将号码存储在我的通话记录中。通话结束后如何从通话记录中删除该号码?


阅读 406

收藏
2020-09-28

共1个答案

一尘不染

我无论如何都使用4.2.2,我不得不修改aftab的代码,因为它对我不起作用。给出我想做的事情可能是一个麻烦的问题,即在传入呼叫结束后立即更新呼叫日志。我认为我必须给操作系统足够的时间来更新表,然后再删除条目,否则它将不存在:

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

并调用我在另一个线程上运行的函数(因为我正在睡觉):

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

添加睡眠后,在通话结束后立即尝试删除时,似乎可以使用。

更新:意识到最后一条评论之后,我们可以在android提供程序调用日志uri上设置contentobserver:

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

现在,我们使用以下方法注册以监听呼叫日志数据库中的更改:

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

然后我们创建一个方法来注册事件或取消注册:

/*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.  
    }  

}
    }
2020-09-28