我正在尝试刷新一个关于BroadcastReceiver的ListView onReceive。在我的主要活动中,其中包含我正在谈论的ListView,我正在注册一个接收器:
private final BroadcastReceiver smsIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String mAction = intent.getAction();
if(mAction.equals(INCOMING_SMS_ACTION)) {
Log.v(runtimeVars.getMyName(), "reloading conversation list");
curConversation = managedQuery(conversationURI, null, null, null, "date DESC");
conversationAdapter = null;
listConversations();
}
}
};
listConversations()看起来像那样
private void listConversations() {
Uri smsURI;
Uri personURI;
Cursor curSms;
Cursor curPerson;
long personID;
curConversation = managedQuery(conversationURI, null, null, null, "date DESC");
conversationArrayList = new ArrayList<ConversationListObj>();
this.conversationAdapter = new ConversationListAdapter(this, R.layout.conversationlist_item, conversationArrayList);
setListAdapter(this.conversationAdapter);
Log.v(runtimeVars.getMyName(), "listConversations");
startManagingCursor(curConversation);
for(curConversation.moveToFirst(); !curConversation.isAfterLast(); curConversation.moveToNext()) {
smsURI = Uri.parse("content://sms/conversations/" + curConversation.getString(curConversation.getColumnIndexOrThrow("thread_id")).toString());
curSms = managedQuery(smsURI, null, null, null, null);
conversationArrayList = new ArrayList<ConversationListObj>();
ConversationListObj newListObj = new ConversationListObj();
newListObj.setThreadID(curConversation.getString(curConversation.getColumnIndexOrThrow("thread_id")).toString());
if (curSms.moveToFirst()) {
personURI = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(curSms.getString(curSms.getColumnIndex("address"))));
curPerson = managedQuery(personURI, new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
startManagingCursor(curPerson);
if(curPerson.moveToFirst()) {
personID = Long.parseLong(curPerson.getString(curPerson.getColumnIndex(ContactsContract.PhoneLookup._ID)));
Bitmap contactPicture = generalMethods.loadContactPhoto(getContentResolver(), personID);
if (contactPicture != null)
newListObj.setSenderPicture(contactPicture);
else {
InputStream input = this.getResources().openRawResource(R.drawable.person_dummy);
newListObj.setSenderPicture(BitmapFactory.decodeStream(input));
}
newListObj.setSenderName(curPerson.getString(curPerson.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)));
}
else {
newListObj.setSenderName(curSms.getString(curSms.getColumnIndex("address")));
}
newListObj.setMessagePart(curConversation.getString(curConversation.getColumnIndexOrThrow("snippet")).toString());
conversationArrayList.add(newListObj);
notifyConversationAdapter();
}
}
curConversation.close();
}
现在,每当我收到短信时,好像我的BroadcastReceiver太快了。在我重新启动应用程序之前,新文本消息不会显示。所以我基本上认为我犯了一个设计错误,但它怎么会是正确的?查看日志文件告诉我
08-29 13:08:44.351: VERBOSE/SotapannaSMS(704): reloading conversation list
08-29 13:08:45.091: VERBOSE/SotapannaSMS(704): listConversations
08-29 13:08:45.921: VERBOSE/Telephony(171): getOrCreateThreadId uri: content://mms-sms/threadID?recipient=5556
08-29 13:08:46.081: INFO/NotificationService(59): enqueueToast pkg=info.SotapannaSMS.messaging callback=android.app.ITransientNotification$Stub$Proxy@43dbff88 duration=0
08-29 13:08:46.571: VERBOSE/Telephony(171): getOrCreateThreadId cursor cnt: 1
在重新启动应用之前,新的短信似乎不在此对话中。我甚至试图让我的应用程序睡3秒钟,看看是否只是等待一会儿,但结果却相同。
我错过了什么?
答案 0 :(得分:1)
Android仅在onReceive完成后才存储消息。
如果您将onReceive呼叫暂停2秒钟,则消息将在2秒后保存。 启动一个新线程,然后在那里睡觉。你的onReceive应尽快返回。
来源:LogCat