Android:为什么广播接收器没有收到意图?
我有一些活动发送给服务的意图。 所有这些都在清单中注册:
<service android:name=".location.LocationService" android:label="@string/location_service_started">
<intent-filter>
<action android:name="@string/location_service_set" />
<action android:name="@string/location_service_start" />
<action android:name="@string/location_service_stop" />
</intent-filter>
</service>
但仅接收 location_service_start 和 location_service_stop 意图。可能是什么原因? 这是我的接收器代码:
private BroadcastReceiver LocationServiceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(getString(R.string.location_service_stop)))
{
showMessage("stop");
}
if(intent.getAction().equals(getString(R.string.location_service_start)))
{
showMessage("start");
}
if(intent.getAction().equals(getString(R.string.location_service_set)))
{
showAlertBox("set");
}
}
};
所以我从未看到“设置”消息。我什至尝试将“开始”和“设置”消息的 sendBroadcast 放在同一个地方,但一切仍然相同。 “开始”- 好的,“设置”- 从未收到。
触发意图的函数:
protected void start()
{
Intent intent = new Intent(getString(R.string.location_service_start));
getApplicationContext().sendBroadcast(intent);
}
protected void set(double lat, double lon, double rad)
{
Intent intent = new Intent(getString(R.string.location_service_set));
intent.putExtra("lat", lat);
intent.putExtra("lon", lon);
intent.putExtra("rad", rad);
getApplicationContext().sendBroadcast(intent);
}
两者都正确发送,没有错误,操作正确。
更新:
噢,是我的错。我忘记添加 filter.addAction... 以获得新意图。 对不起。但答案确实很有用!谢谢!
I have a few intents that activity sends to service.
All of those are registered in manifest:
<service android:name=".location.LocationService" android:label="@string/location_service_started">
<intent-filter>
<action android:name="@string/location_service_set" />
<action android:name="@string/location_service_start" />
<action android:name="@string/location_service_stop" />
</intent-filter>
</service>
But only location_service_start and location_service_stop intents are received. What could be the reason?
There is my receiver code:
private BroadcastReceiver LocationServiceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(getString(R.string.location_service_stop)))
{
showMessage("stop");
}
if(intent.getAction().equals(getString(R.string.location_service_start)))
{
showMessage("start");
}
if(intent.getAction().equals(getString(R.string.location_service_set)))
{
showAlertBox("set");
}
}
};
So I never see "set" message. I've even tried put sendBroadcast for "start" and "set" messages in the same place, but everything still the same. "start" - OK, "set" - never received.
Functions that fires intents:
protected void start()
{
Intent intent = new Intent(getString(R.string.location_service_start));
getApplicationContext().sendBroadcast(intent);
}
protected void set(double lat, double lon, double rad)
{
Intent intent = new Intent(getString(R.string.location_service_set));
intent.putExtra("lat", lat);
intent.putExtra("lon", lon);
intent.putExtra("rad", rad);
getApplicationContext().sendBroadcast(intent);
}
Both are correct send, without errors, actions are correct.
UPD:
Oh, my fault. I forgot to add filter.addAction... for new intent.
I'm sorry. But answers was really useful! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您不会在
中将字符串资源用于操作字符串,因为您永远不想将它们国际化。通常,您根本不会在服务中使用
,除非您将该服务公开给第三方应用程序。事实上,现在您正在向第三方应用程序公开您的服务,因此任何人都可以将这些命令发送到您的服务。不,服务没有接收到它们。您正在 Java 代码中发送广播。服务不接收广播。
除非您知道自己在做什么,否则不要使用
getApplicationContext()
。无论您在上调用getApplicationContext()
,都是Context
,因此您只需对其调用sendBroadcast()
即可。Generally, you do not use string resources for action strings in an
<intent-filter>
, because you never want to internationalize them.Generally, you do not use an
<intent-filter>
at all with a service unless you are exposing that service to third-party apps. In fact, right now, you are exposing your service to third-party apps, so anyone can send these commands to your service.No, none of them are received by the service. You are sending broadcasts in the Java code. Services do not receive broadcasts.
Do not use
getApplicationContext()
unless you know what you are doing. Whatever you are callinggetApplicationContext()
on is aContext
, so you can just callsendBroadcast()
on it.复制并粘贴我刚刚回答的这个问题。应该是同样的问题。
您必须将每个
标记放入单独的
标记中显现。这应该是一个错误,因为文档指出您可以在过滤器标记中放置多个操作:
来源
Copy & Paste from this question I just answered. Should be the same issue.
You have to put each
<action />
tag inside a seperate<intent-filter />
tag in your manifest.This should be a bug, since the doc states you can put more than one action inside a filter tag:
Source