我正在从收件箱中读取前 100 条短信并将其存储在本地数据库中。我在 AsyncTask 的帮助下做到了这一点。之后,我正在从本地数据库中读取这些 SMS 并显示它。在读取和存储前 100 条短信后,在 AysncTask 的 onPostExecute 中,我正在调用该服务来读取剩余的短信并将其存储在本地数据库中。在调用服务之前,UI 是响应式的,但在调用服务后,UI 变得无响应。是由于数据库锁定还是其他原因。请帮忙解决这个问题...
我的 AsyncTask 代码
private class InitialSetup extends AsyncTask<String, Integer, Long> {
@Override
protected Long doInBackground(String... urls) {
fetchSMS();
updateDatabase();
return 0L;
}
@Override
protected void onPostExecute(Long result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
populateUI(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), SMSService.class);
getApplication().startService(intent);
}
}
服务代码
public class SMSService extends Service {
Long inboxSMSID, localSMSID;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startid) {
if (intent.hasExtra("InboxSMS") && intent.hasExtra("SMSID")) {
inboxSMSID = intent.getLongExtra("InboxSMS", 0);
localSMSID = intent.getLongExtra("SMSID", 0);
globalToLocalSMSDB(inboxSMSID);
}
}
private void globalToLocalSMSDB(Long id) {
SMSTable smsObj = new SMSTable(getApplicationContext());
smsObj.open();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor smscur = getContentResolver().query(uriSMSURI, null,
"_id < " + id, null, "_id DESC");
while (smscur.moveToNext()) {
String sender = SMSManagement.getSender(smscur);
String message = SMSManagement.getMessage(smscur);
String timeStamp = SMSManagement.getTime(smscur);
smsObj.insertIntoSMSTable(sender, message, timeStamp);
}
smscur.close();
smsObj.close();
}
}