android - Using a broadcast intent/broadcast receiver to send messages from a service to an activity -
So I understand (I think) about broadcasting purposes and to get them the message.
So now, my problem / which I can not work for is to send a message to the effect of a receiver for How do I send a message to an activity? I have to instantinate the receiver in the activity that I want to send messages and monitor it in some way? what else? I understand this concept, but in reality there is no application. Any help will be absolutely amazing, thanks. edit Then, declare square variables like ... Then ... and in And that's about it, make 'action' unique for your package / app, i.e., onReiveive , say that I have a receiver :
Public Class Receive Message Broadcast Extends Expand Receiver {@ Override Public Records on Receive (context reference, intent intent) {string action = intent.action}; If (action.equalsIgnoreCase (TheService.DOWNLOADED)) Send a message to {// activity}}}
Broadcast receiver Corrected examples and manual declarations for registration / unregistered were also deleted. Define
get message as an internal class within
activity for which to hear messages from
service Is required.
get message myReceiver = null; Boolean my receiver ISR registered = false; Use
onCreate () in
myReceiver = new ReceiveMessages ();
onResume () ...
(MyReceiverIsRegistered) {Register Receiver (myReceiver, New IntentFilter (" Com.mycompany.myapp.SOME_MESSAGE ")); MyReceiverIsRegistered = True; }
onPause () ...
if (myReceiverIsRegistered) {unregistered receiver (myReceiver) ; MyReceiverIsRegistered = false; } Create and broadcast
intent in
service ...
Intent I = new intention ("Com.mycompany.myapp.SOME_MESSAGE"); SendBroadcast (i);
com.mycompany ... as my example. This helps avoid situations where other apps or system components may try to process it.
Comments
Post a Comment