使用contentResolver删除短信太慢 [英] Delete SMS with contentResolver is too slow

查看:115
本文介绍了使用contentResolver删除短信太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除手机上的所有短信,但每次对话都只能删除最近的500条短信.这是我的代码,但是非常慢(删除一条短信大约需要10秒钟).我如何加快此代码的速度:

I would like to delete all SMS on my phone except the 500 last SMS for each conversation. This is my code but it's very slow (take about 10 seconds to delete one SMS). How I can speed up this code :

    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();

推荐答案

您可以做的主要事情之一是

One of the main things you can do is batch ContentProvider operations instead of doing 33,900 separate deletes:

// 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历史记录进行一次批处理操作.

Up you whether you want to do one batch operation per conversation or one batch operation for the entire SMS history.

这篇关于使用contentResolver删除短信太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆