Reputation: 73721
Typically you start a service like this
Intent i = new Intent(context,MessageService.class);
context.startService(i);
but what I want to do is send an intent that was received in a BroadcastReceiver
to a service. If I start a service the way shown above that wont get the intent from the BroadcastReceiver
correct?
Basically I just want my BroadcastReceiver
to start my service and then let the service itself handle what kind of intent was received
is this possible?
Upvotes: 4
Views: 7738
Reputation: 132972
Send Intent from BroadcastReceiver to Service as:
Intent intent = new Intent(context,MessageService.class);
String value = "String you want to pass";
String name = "data";
intent.putExtra(name, value);
context.startService(intent);
Reciver Intent in onStartCommand
method of service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
if (intent.getStringExtra("data") != null) {
{
String str=intent.getStringExtra("data");//get data here sended from BroadcastReceiver
}
return super.onStartCommand(intent,flags,startId);
}
for how we communicate between Service and BroadcastReceiver see this post
Upvotes: 5