android - 如何启动基于 ServiceInfo 对象的服务?

标签 android service

在我的应用程序中,我查询在其 intent-filters 中具有特定类别的服务列表。这很好,我得到了一个包含 ResolveInfo 对象的列表。在这些 ResolveInfo 中,我找到了“serviceInfo”字段,它应该描述找到的服务的详细信息。

现在我如何从 serviceInfo 构造一个 Intent,它可以启动找到的服务?

我现在的代码是这样的:

PackageManager pm = getApplicationContext().getPackageManager();
  Intent i = new Intent();
  i.setAction("<my custom action>");
  i.addCategory("<my custom category>");

  List<ResolveInfo> l = pm.queryIntentServices(i, 0);

  gatherAgentNum = l.size();

  if(gatherAgentNum > 0){
   for(ResolveInfo info : l){
    Intent i2 = new Intent(this, info.serviceInfo.getClass());
    i2.putExtra(BaseAgent.KEY_RESULT_RECEIVER, new GatherResult(mHandler));
    startService(i2);
   }
  }

这显然是错误的,“info.serviceInfo.getClass()”只是返回 serviceInfo 对象的类。谁能帮我解决这个问题?

谢谢

编辑:解决方案(至少是我使用的那个):

PackageManager pm = getApplicationContext().getPackageManager();
        Intent i = new Intent();
        i.setAction("<my action>");
        i.addCategory("<my category>");

        List<ResolveInfo> l = pm.queryIntentServices(i, 0);

        if(l.size() > 0){
            for(ResolveInfo info : l){
                ServiceInfo servInfo = info.serviceInfo;
                ComponentName name = new ComponentName(servInfo.applicationInfo.packageName, servInfo.name);

                Intent i2 = new Intent();
                i2.setComponent(name);

                startService(i2);
            }
        }

最佳答案

关于android - 如何启动基于 ServiceInfo 对象的服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291027/

相关文章:

java - 找不到 Android CalendarView 类

java - 我正在尝试将数据输入到 parse.com 中的 custommom 列,但出现错误

android - 可以在服务中扩展 XML 布局吗? (安卓)

java - 服务层和 DAO 层的职责和使用

java - 通过命令提示符将java程序安装为Windows服务

android - 无法在 Android 2.2 中使 ListView 背景透明

android - Android Studio 应用检查器的问题

android - Groups.rotation 有问题吗?

asp.net - 在 ASMX Web 服务中对同一 WebMethod 使用 POST 和 GET Ajax 调用

angularjs - 在 Angular 模型(ajax 调用所在的 Angular 服务)中检索当前域以准备完整的 api url 以检索数据