Pass A Value From Activity To Broadcastreceiver And Start A Service From The Broadcast Receiver
I have an activity. It contains a button whose text changes dynamically. I would like to pass this text to my broadcast receiver which receives the sms. Now my broadcast receive
Solution 1:
if your BroadcastReceiver is defined in a separate class file, then you may simply broadcast the value to that receiver. Once the value is received, do the magic for service by using receiver's context
Update:
in your activity:
Intentin=newIntent("my.action.string");
in.putExtra("state", "activated");
sendBroadcast(in);
in your receiver:
@OverridepublicvoidonReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("my.action.string")){
String state = intent.getExtras().getString("state");
//do your stuff
}
}
in manifest xml:
<receiverandroid:name=".YourBroadcastReceiver"android:enabled="true"><intent-filter><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" /><actionandroid:name="my.action.string" /><!-- and some more actions if you want --></intent-filter></receiver>
Solution 2:
You can have your activity send an intent to the receiver, and pass the text as an extra
Intent i= newIntent(this, YourReceiver.class);
i.putExtra("txt", "the string value");
startActivity(i)
And then in your receiver, start the service using the startService function
Post a Comment for "Pass A Value From Activity To Broadcastreceiver And Start A Service From The Broadcast Receiver"