[英]Sending a BroadcastReceiver intent to a service
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. 但我想要做的是将
BroadcastReceiver
中收到的意图发送给服务。 If I start a service the way shown above that wont get the intent from the BroadcastReceiver
correct? 如果我按照上面显示的方式启动服务,不会从
BroadcastReceiver
正确的意图?
Basically I just want my BroadcastReceiver
to start my service and then let the service itself handle what kind of intent was received 基本上我只是希望我的
BroadcastReceiver
启动我的服务,然后让服务本身处理收到的意图
is this possible? 这可能吗?
Send Intent from BroadcastReceiver to Service as: 从BroadcastReceiver发送Intent到服务:
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: 在
onStartCommand
服务方法中的Reciver Intent:
@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 关于我们如何在Service和BroadcastReceiver之间进行通信,请参阅此帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.