Delete SMS with contentResolver is too slow

后端 未结 1 1159
不知归路
不知归路 2021-01-25 00:05

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 ca

相关标签:
1条回答
  • 2021-01-25 00:45

    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
    }
    

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

    0 讨论(0)
提交回复
热议问题