具有相同意图过滤器的两个服务 [英] two service with the same intent filter

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

问题描述

我在设备上安装了两个应用程序,每个应用程序中都有一个服务组件,并且这两个服务具有相同的意图过滤器声明,如下所示:

I have two applications installed on my device,each with a service component in it and these two service has the same intent filter declaration,like this:

<intent-filter>
<action android:name="com.example.intent.action.SHOW"/>
</intent-filter>

然后我以这种方式启动服务:

And I start the service in this way:

Intent intent = new Intent();
intent.setAction("com.example.intent.action.SHOW");
startService(intent);

我发现这两个服务之一启动了,但是我不确定这是怎么发生的.众所周知,如果我们用相同的intent过滤器声明编写两个活动,则会弹出一个对话框,让用户选择一个活动来完成操作.让我感到困惑的是,Android如何在具有相同意图过滤器的人中选择要启动的服务,制定此决定的策略是什么?

I found that one of these two services started,but I am not sure how this happened.As we know,if we write two activities with the same intent filter declaration,a dialog will be poped up and let the user chooses a activity to complete the action.What got me confused is that how Android chooses the service to be started in these who has the same intent filters,what's the strategy of making this decision?

提前谢谢!

更新:
尤里(Yury)是对的,这是ICS上来自frameworks/base/services/java/com/android/server/pm/PackageMangerService.java的代码片段:

update:
Yury is right, here is code snippet from frameworks/base/services/java/com/android/server/pm/PackageMangerService.java on ICS:

public ResolveInfo resolveService(Intent intent, String resolvedType,
    int flags) {
List<ResolveInfo> query = queryIntentServices(intent, resolvedType,
        flags);
if (query != null) {
    if (query.size() >= 1) {
        // If there is more than one service with the same priority,
        // just arbitrarily pick the first one.
        return query.get(0);
    }
}
return null;
}

如我们所见,如果有多个服务与请求的意图相匹配,Android将任意选择一项来启动.但是,实际上将启动哪个服务是意外的.

As we can see,if there are more than one service match the requested intent, Android will arbitrarily pick one to start. But, which service will be actually started is unexpected.

推荐答案

如果有多个服务具有相同的意图过滤器,则Android OS会随机选择这些服务之一并将其意图传递给它.

If there are more than 1 Service with the same intent-filter then Android OS randomly selects one of these Services and pass to it the intent.

这篇关于具有相同意图过滤器的两个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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