呼叫结束后从呼叫记录中删除呼叫

时间:2011-03-19 10:08:21

标签: android calling-convention

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

4 个答案:

答案 0 :(得分:5)

首先,您必须设置广播接收器以检测手机状态。这是完全相同的问题:Stackoverflow - Intent to be fired when a call ends?

现在要删除通话记录条目,这是Google上的第一个链接:Call log deletion in Android
在示例中,他删除了特定号码的所有呼叫条目,但您可以更改查询以删除特定呼叫日志ID的条目。

希望这会有所帮助。 干杯

答案 1 :(得分:4)

我使用4.2.2无论如何我必须修改aftab的代码,因为它不适合我。这可能是一个asych问题,我试图做的是在来电结束后立即更新通话记录。我想在删除条目之前我必须给O / S足够的时间来更新表格,否则它就不存在了:

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

}
    }

答案 2 :(得分:2)

我只是尝试这种方法,它在我的HTC 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 ");
            }
        }

答案 3 :(得分:2)

早期回答者的修改版本。你真的不需要一个while循环。您也不需要Thread.sleep(4000),为ContentObserver注册CallLog.Calls.CONTENT_URI,并在onChange中调用以下方法。但在致电之前请确保取消注册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());
        }
    }