Android:为什么广播接收器没有收到意图?

发布于 2024-11-28 17:35:47 字数 1989 浏览 0 评论 0原文

我有一些活动发送给服务的意图。 所有这些都在清单中注册:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-12-05 17:35:47

所有这些都在清单中注册:

通常,您不会在 中将字符串资源用于操作字符串,因为您永远不想将它们国际化。

通常,您根本不会在服务中使用,除非您将该服务公开给第三方应用程序。事实上,现在您正在向第三方应用程序公开您的服务,因此任何人都可以将这些命令发送到您的服务。

但仅收到 location_service_start 和 location_service_stop 意图

不,服务没有接收到它们。您正在 Java 代码中发送广播。服务不接收广播。

激发意图的函数:

除非您知道自己在做什么,否则不要使用 getApplicationContext()。无论您在上调用getApplicationContext(),都是Context,因此您只需对其调用sendBroadcast()即可。

All of those are registered in manifest:

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.

But only location_service_start and location_service_stop intents are received

No, none of them are received by the service. You are sending broadcasts in the Java code. Services do not receive broadcasts.

Functions that fires intents:

Do not use getApplicationContext() unless you know what you are doing. Whatever you are calling getApplicationContext() on is a Context, so you can just call sendBroadcast() on it.

甲如呢乙后呢 2024-12-05 17:35:47

复制并粘贴我刚刚回答的这个问题。应该是同样的问题。

您必须将每个 标记放入单独的 标记中显现。

这应该是一个错误,因为文档指出您可以在过滤器标记中放置多个操作:

应包含零个或多个操作 [..] 标签
里面描述过滤器的内容。

来源

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:

Zero or more action [..] tags should be included
inside to describe the contents of the filter.

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文