Search code examples
androidandroid-broadcastreceiver

How to use listner inside a BroadCastRecievever?


I have to display number of times device is unlocked with a textView which is inside my Activity. I am storing the unlock frequency in Prefrences with BroadCast Reciever USER_ACTIVE onReciever().

I want to update the Activity textView everyTime i recieve the User_Active broadcast.

Any leads?


Solution

  • Simple three steps:

    1. Create a runtime broadcast receiver with your own action(Ex: UPDATED_TEXT_PREFERENCES).
    2. Register/unregister for broadcast in onStart() and onStop()
    3. On your USER_ACTIVE onReciever(). send a broadcast with the own action you have created(Ex: UPDATED_TEXT_PREFERENCES)
    4. On your activity onReceive you can update your textview.

    Comment below for any doubts.