我想删除我的手机上的所有短信,除了500条最后的短信为每次交谈。这是我的代码,但它是非常慢(约10秒删除一条短信)。我如何加快这段代码的速度:
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();发布于 2014-01-14 21:02:49
您可以做的主要事情之一是批处理ContentProvider操作,而不是执行33,900个单独的删除:
// 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历史记录执行一次批处理操作,还是每次会话执行一次批处理操作。
https://stackoverflow.com/questions/21121848
复制相似问题