How to use LocalBroadcastManager?

后端 未结 13 3210
天命终不由人
天命终不由人 2020-11-21 04:15

How to use/locate LocalBroadcastManager as described in google docs and Service broadcast doc?

I tried to google it, but there is no code available to s

13条回答
  •  眼角桃花
    2020-11-21 04:53

    How to change your global broadcast to LocalBroadcast

    1) Create Instance

    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
    

    2) For registering BroadcastReceiver

    Replace

    registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
    

    With

    localBroadcastManager.registerReceiver(new YourReceiver(),new IntentFilter("YourAction"));
    

    3) For sending broadcast message

    Replace

    sendBroadcast(intent);
    

    With

    localBroadcastManager.sendBroadcast(intent);
    

    4) For unregistering broadcast message

    Replace

    unregisterReceiver(mybroadcast);
    

    With

    localBroadcastManager.unregisterReceiver(mybroadcast);
    

提交回复
热议问题