2014-09-30 23 views
0

我想「重置」所有丟失的電話,到目前爲止,我沒有設置新的未接來電「老」

public void resetMissingCalls() { 
    Runnable reset = new Runnable() { 

     @Override 
     public void run() { 
      Utils.PrintInfo("resetMissingCalls.run"); 
      String[] projection = { CallLog.Calls._ID, 
        CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE }; 
      String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE 
        + " AND " + CallLog.Calls.NEW + "=1"; 
      Cursor c = getContentResolver().query(
        CallLog.Calls.CONTENT_URI, projection, where, null, null); 
      Utils.PrintInfo("CALLS " + c.getCount()); 

      c.moveToFirst(); 
      if (c.getCount() > 0) { 
       do { 
        Utils.PrintInfo("coursor"); 
        setAsRead(c.getString(c 
          .getColumnIndex(CallLog.Calls._ID))); 
       } while (c.moveToNext()); 
      } 
      c.close(); 
     } 
    }; 
    new Handler().post(reset); 
} 

public void setAsRead(String id) { 
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    // Number 
    Builder builder = ContentProviderOperation 
      .newUpdate(CallLog.Calls.CONTENT_URI); 
    builder.withSelection(
      ContactsContract.Data.CONTACT_ID + "=?", 
      new String[] { 
        id }); 
    builder.withValue(CallLog.Calls.NEW, "0"); 
    ops.add(builder.build()); 

    // Asking the Contact provider to create a new contact 
    try { 
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Utils.PrintError("Exception: " + e.getMessage()); 
    } 
} 

然而getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);拋出一個異常

java.lang.UnsupportedOperationException:URI:內容:// call_log /呼叫,主叫用戶:com.test.missing調用包:com.test.missing

我都使用的許可

<uses-permission android:name="android.permission.READ_CALL_LOG" /> 
<uses-permission android:name="android.permission.WRITE_CALL_LOG" /> 

回答

0

我已經自己修復了,只是改setAsRead方法爲

public void setAsRead(String id) {  
    ContentValues values = new ContentValues(); 
    values.put(CallLog.Calls.NEW, Integer.valueOf(0)); 

    String[] fv = new String[] { id }; 

    getContentResolver().update(CallLog.Calls.CONTENT_URI, values, 
      CallLog.Calls._ID + "= ?", fv); 
}