如何使一个隐含的意图明确? [英] How to make an implicit intent explicit?

查看:157
本文介绍了如何使一个隐含的意图明确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序,A和B,它们使用的是Android库C. B的服务A想通过C使用的,例如

I have two applications, A and B, which use a android library C. B has a service A wants to use via C, e.g.

<service android:name="my.package.in.a.service.ConnectorService"
         android:exported="true">
    <intent-filter>
        <action android:name="my.package.in.a.action.START_A"/>
    </intent-filter>
</service>

在我的图书馆有它试图将其绑定到服务类,例如

In my library there is a class which tries to bind it to the service, e.g.

    Intent intent = new Intent("my.package.in.a.service.ConnectorService");
    /** establish a connection with the service. */
    boolean result = context.bindService(intent, messenger,
         Context.BIND_AUTO_CREATE);

显然,你不能这样做的了,因为安全问题(隐与显式意图)。我试着用初始化A中定义的操作我也试图添加包名的意图,并尝试设置类的名字,例如

Apparently, you can't do that anymore because of security issues (implicit vs. explicit intents). I've tried to initialise the intent using the action defined in A. I've also tried to add the package name and also tried to set the class name, e.g.

   Intent intent = new Intent()
   intent.setPackage("my.package.in.a.service");
   intent.setClassName("my.package.in.a.service",
            "my.package.in.a.service.ConnectorService");

我已经ALS试图找到使用软件包管理器的服务,例如。

I've als tried to find the service using the package manager, e.g.

   Intent intent = new Intent("my.package.in.a.service.ConnectorService");
   List<ResolveInfo> resolveInfoList = context.getPackageManager()
            .queryIntentServices(intent, Context.BIND_AUTO_CREATE);

    if (resolveInfoList.isEmpty()) {
        Log.e(TAG, "could not find any service");
    }

    if (resolveInfoList.size() > 1) {
        Log.e(TAG, "multiple services found");
    }

我有点纳闷,我做错了什么?据我了解它,你可以做一个隐含的意图明确,即使它是不一样的封装/应用程序的一部分,简单的通过指定的包/类名。然而,这一切似乎都失败,很明显我做错了什么?

I'm a bit puzzled what I'm doing wrong? As far as I understood it you can make an implicit intent explicit, even while it is not part of the same package/application, by simply specifying the package/classname. However, all this seems to fail and obviously I'm doing something wrong?

推荐答案

我不认为 setPackage()没有足够,使其充分明确。对于这一点,你需要 setComponent()

I don't think that setPackage() does "enough" to make it fully explicit. For that, you need setComponent().

你的第二个方法,使用软件包管理系统,是在正确的轨道上,但除非你只是截断你的code这个问题上,你错过了调整,将 - 意图一步。

Your second approach, using PackageManager, is on the right track, but unless you just truncated your code listing, you missed the adjust-the-Intent step.

从的 =htt​​ps://commonsware.com/Android相对=nofollow>这本书,我不仅检查单的服务实现的,但我也检查其签名(以确保我想要的应用程序绑定到是不是黑客攻击),并调整意图

In this sample app from this book, I not only check for a single implementation of the service, but I also check its signature (to ensure the app I want to bind to is not hacked) and adjust the Intent:

Intent implicit=new Intent(IDownload.class.getName());
List<ResolveInfo> matches=getActivity().getPackageManager()
  .queryIntentServices(implicit, 0);

if (matches.size() == 0) {
  Toast.makeText(getActivity(), "Cannot find a matching service!",
    Toast.LENGTH_LONG).show();
}
else if (matches.size() > 1) {
  Toast.makeText(getActivity(), "Found multiple matching services!",
    Toast.LENGTH_LONG).show();
}
else {
  ServiceInfo svcInfo=matches.get(0).serviceInfo;

  try {
    String otherHash=SignatureUtils.getSignatureHash(getActivity(),
      svcInfo.applicationInfo.packageName);
    String expected=getActivity().getString(R.string.expected_sig_hash);

    if (expected.equals(otherHash)) {
      Intent explicit=new Intent(implicit);
      ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName,
        svcInfo.name);

      explicit.setComponent(cn);
      appContext.bindService(explicit, this, Context.BIND_AUTO_CREATE);
    }
    else {
      Toast.makeText(getActivity(), "Unexpected signature found!",
        Toast.LENGTH_LONG).show();
    }
  }
  catch (Exception e) {
    Log.e(getClass().getSimpleName(), "Exception trying to get signature hash", e);
  }
}

这篇关于如何使一个隐含的意图明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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