我正在尝试使用 BroadcastReceiver 从服务接收新消息。但 BroadcastReceiver 总是收到广播两次...接收器代码:
private class RecentBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle messageBundle = intent.getExtras();
IChatCSMessage message = (IChatCSMessage) messageBundle.get("message");
String sender = message.getSender().getUserId();
addRecentBuddy(sender.trim());
recentBuddyAdapter.notifyDataSetChanged();
allMessages.get(sender).add(message);
Log.i("RECENT_RECEIVER", "RECEIVING MESSAGE:" + message.getMessage());
}
}
public void initReceiver() {
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("com.ichat.message");
recentBroadcastReceiver = new RecentBroadcastReceiver();
registerReceiver(recentBroadcastReceiver, myIntentFilter);
}
广播是:
Intent intent = new Intent("com.ichat.message");
intent.putExtra("message", message);
IChatClientService.this.sendBroadcast(intent);
谁能帮我?