2014-01-14 49 views
1

我想删除手机上的所有短信,除了每次通话的最后一个短信500以外。 这是我的代码,但速度很慢(大约需要10秒钟才能删除一条短信)。 我如何可以加快这个代码:删除带contentResolver的短信太慢

ContentResolver cr = getContentResolver(); 
    Uri uriConv = Uri.parse("content://sms/conversations"); 
    Uri uriSms = Uri.parse("content://sms/"); 
    Cursor cConv = cr.query(uriConv, 
      new String[]{"thread_id"}, null, null, null); 

    while(cConv.moveToNext()) { 
     Cursor cSms = cr.query(uriSms, 
       null, 
       "thread_id = " + cConv.getInt(cConv.getColumnIndex("thread_id")), 
       null, "date ASC"); 
     int count = cSms.getCount(); 
     for(int i = 0; i < count - 500; ++i) { 
      if (cSms.moveToNext()) { 
       cr.delete(
         Uri.parse("content://sms/" + cSms.getInt(0)), 
         null, null); 
      } 
     } 
     cSms.close(); 
    } 
    cConv.close(); 
+0

你有多少次会话?你做删除时每次对话有多少短信? – kAnNaN

+0

我有约34000短信和约100对话。但是有一个对话有26000短信 – Guillaume

+0

我想它是一个相当不错的时间考虑你在数据库上做一个条件双光标操作。它也取决于您使用的移动设备。 – kAnNaN

回答

4

,您所要做的主要事情是batch ContentProvider operations而不是做33900单独删除:

// Before your loop 
ArrayList<ContentProviderOperation> operations = 
    new ArrayList<ContentProviderOperation>(); 

// Instead of cr.delete use 
operations.add(new ContentProviderOperation.newDelete(
    Uri.parse("content://sms/" + cSms.getInt(0)))); 

// After your loop 
try { 
    cr.applyBatch("sms", operations); // May also try "mms-sms" in place of "sms" 
} catch(OperationApplicationException e) { 
    // Handle the error 
} catch(RemoteException e) { 
    // Handle the error 
} 

截至您是否想要做一个批量操作每个会话或整个SMS历史的批量操作。