意图过滤器无法解决外部服务 [英] External service can not be resolved by intent filter

查看:99
本文介绍了意图过滤器无法解决外部服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我缺少一些重要的东西或...

I am missing something essential or...

我有两个应用程序,其中一个包含在AndroidManifest中声明的带有意图过滤器的导出服务:

I have two applications, one of them containing exported service with intent filter declared in AndroidManifest:

<service
    android:name=".MyService"
    android:exported="true"
    android:permission="my.permission" >
    <intent-filter>
        <action android:name="my.service" />
    </intent-filter>
</service>

我可以使用"my.service"操作从另一个应用程序成功绑定到该服务.但是我想启动服务(向它发送命令).如果我写:

I can successfully bind to that service from another application by using "my.service" action. But I want to start service (send command to it). If I write:

Intent intent = new Intent("my.service");
intent.setAction("my.command");
ComponentName cn = startService(intent);

我在cn中得到了null(服务无法解析).但是,如果我将其更改为:

I get null in cn (service can not be resolved). But if I change this to:

PackageManager packageManager = getPackageManager();
Intent serviceIntent = new Intent("my.service");
List<ResolveInfo> services = packageManager.queryIntentServices(serviceIntent, 0);
if (services.size() > 0) {
    ResolveInfo service = services.get(0);
    intent = new Intent();
    intent.setClassName(service.serviceInfo.packageName, service.serviceInfo.name);
    intent.setAction("my.command");
    ComponentName cn = startService(intent);
}

服务已成功启动.我在包含第一个变体的StackOverflow上看到了很多建议,但是我无法使其正常工作.有什么想法吗?

Service is successfully started. I've seen many advices on StackOverflow containing first variant, but I can not make it work. Any ideas?

推荐答案

在无效的代码中,您首先将构造函数中的操作设置为"my.service".这将起作用,但是在下一行中,您将操作设置为"my.command".如果您需要向同一服务发送其他操作,则还必须将该操作添加到清单中的意图过滤器中.

In your code that isn't working, you are first setting your action in the constructor to "my.service". This would work, however in the next line, you are setting your action to "my.command". If you need to send a different action to the same service you will have to also add that action to your intent-filter in your manifest:

<service
    android:name=".MyService"
    android:exported="true"
    android:permission="my.permission" >
    <intent-filter>
        <action android:name="my.service" />
        <action android:name="my.command" />
    </intent-filter>
</service>

这篇关于意图过滤器无法解决外部服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆