首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用contentResolver删除短信太慢了

用contentResolver删除短信太慢了
EN

Stack Overflow用户
提问于 2014-01-14 19:06:23
回答 1查看 1.1K关注 0票数 1

我想删除我的手机上的所有短信,除了500条最后的短信为每次交谈。这是我的代码,但它是非常慢(约10秒删除一条短信)。我如何加快这段代码的速度:

代码语言:javascript
复制
    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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-14 21:02:49

您可以做的主要事情之一是批处理ContentProvider操作,而不是执行33,900个单独的删除:

代码语言:javascript
复制
// 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历史记录执行一次批处理操作,还是每次会话执行一次批处理操作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21121848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档