gpt4 book ai didi

android - 使用 contentResolver 删除短信太慢

转载 作者:行者123 更新时间:2023-11-29 16:04:00 29 4
gpt4 key购买 nike

我想删除我手机上的所有短信,但每次对话的最后 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();

最佳答案

您可以做的主要事情之一是 batch ContentProvider operations而不是进行 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 历史记录进行一次批量操作。

关于android - 使用 contentResolver 删除短信太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121848/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com