权限
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
理解
1、sendStickyBroadcast 发出的最后一个Intent会被保留,下次当Recevier处于活跃的 时候,又会接受到它。
2、sendBroadcast中发出的intent在ReceverActivity不处于onResume状态是无 法接受到的,即使后面再次使其处于该状态也无法接受到。
场景
当UI还没有打开,已经接收到了一条广播。即时通讯用的比较多。
片段
roster.addRosterListener(new RosterListener() {
@Override
public void presenceChanged(Presence presence) {
String mood = presence.getStatus() == null ? "" : presence.getStatus();
MyRoster myRoster = new MyRoster();
myRoster.setJid(presence.getFrom());
myRoster.setOnoffline(presence.getType().toString());//出席状态(在线或不在线)
myRoster.setMood(mood);//可以理解为心情(在线时候的状态)
Bundle bundle = new Bundle();
bundle.putParcelable(CoreServiceUtil.ACTION_ROSTER_PRESENCE_CHANGE,
myRoster);
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setAction(CoreServiceUtil.ACTION_ROSTER_PRESENCE_CHANGE);
coreService.sendStickyBroadcast(intent);
}
});
清除
context.removeStickyBroadcast(new Intent(ACTIION);