sendStickyBroadcast && sendBroadcast

权限

<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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值