Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an Android application for which I need to insert SMS messages to the Android device. I am facing performance issues with my code.

Currently, my code is taking 120 seconds to insert 500 messages while "SMS Backup and Restore" application takes 80 seconds to insert 500 messages.

Code:
Java
Long start_time = System.currentTimeMillis();

 //fetch thread id
 Uri.Builder builder = thread_uri.buildUpon();
builder.appendQueryParameter("recipient", values.getAsString("address"));
Uri uri = builder.build();

Long threadId = 0L;
 Cursor cursor = sms.query(uri, new String[]{"_id"},
                                            null, null, null);

if (cursor.moveToFirst()) {
threadId = cursor.getLong(0);
}
cursor.close();
Log.d("nims","time to thread fetch:"+(System.currentTimeMillis()-start_time));

values.put("thread_id", threadId);
start_time = System.currentTimeMillis();

//insert SMS
sms.insert(sms_uri, values);
 Log.d("nims","time to insert main sms:"+(System.currentTimeMillis()-start_time));

 //insert dummy sms
start_time = System.currentTimeMillis();
ContentValues dummyValues = new ContentValues();
dummyValues.put("thread_id", threadId);
Uri dummySms = sms.insert(temp_sms_uri, dummyValues);

 //Delete dummy sms
 sms.delete(dummySms, null, null);

Log.d("nims","time to insert and delete temp sms:"+(System.currentTimeMillis()-start_time));


I have following questions:

1. Why there is a change in insert time for SMS even if the SMS data is same?
2. What are the possible reasons for the performance drop of SMS insert code?
3. What alternate solutions I can try to optimize the performance?

I’d appreciate any suggestions on this issue. Thank you.

What I have tried:

Code debugging results:

1. I found that the code takes different time to insert individual SMS messages. I found that the insert time varies from 50 ms to 700 ms.
2. Execution time to delete dummy SMS varies from 70 ms to 350 ms.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900