2011-03-19 162 views

回答

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

即時通訊使用4.2.2反正我有,因爲它不是爲我工作,修改阿夫塔卜的代碼。這可能是一個淺薄的問題,我想要做的就是在傳入呼叫結束後立即更新通話記錄。我想我得給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(); 

加入睡眠後,在通話結束後嘗試刪除時似乎工作。

UPDATE:後最後的評論意識到,我們可以建立在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);

然後我們做的方法要麼事件或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. 
    } 

} 
    } 
+0

它不工作,有什麼想法?它確實調用了刪除功能,布爾值爲true,但是號碼在日誌歷史記錄中,因爲 – 2016-11-14 07:29:35

+0

是否設置了contentObserver?通話記錄發生變化時,您是否收到了發送給您的事件? – j2emanue 2016-11-28 00:27:13

2

早期answsers的修改版本。你並不需要一段時間循環。您也不需要Thread.sleep(4000),註冊ContentObserverCallLog.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()); 
     } 
    } 
+0

關於內容觀察者的絕妙想法,我會努力讓你註冊並更新我的評論以幫助他人。 – j2emanue 2013-08-22 17:17:53

+0

爲什麼你需要一個遊標循環。 'ContentResolver.delete()'可以刪除多個記錄,我錯了嗎? 'getContentResolver()。delete(CallLog.Calls.CONTENT_URI,CallLog.Calls.NUMBER +「=?」,new String [] {phoneNumber});'應該可以 – osrl 2015-12-05 18:42:18