【问题标题】:How do I start a service which is defined in a different package?如何启动在不同包中定义的服务?
【发布时间】:2010-04-25 16:25:26
【问题描述】:

我有两个应用程序,一个在命名空间 com.gtosoft.voyager 中运行,另一个是 com.gtosoft.dash。我想从 com.gtosoft.dash 启动在 com.gtosoft.voyager 中定义的服务...

我认为我需要一个意图,但是在使用 startService() 启动意图之前,我会传递哪些参数?

如果它们在同一个包中,我可以使用

Intent svc=new Intent (SettingsActivity.this,VoyagerService.class);
startService(svc);

定义服务的清单片段

<application android:icon="@drawable/voyagercarlogo" android:label="@string/app_name" android:debuggable="false">

    <provider android:name="com.gtosoft.voyager.VoyagerCProvider" 
        android:authorities="com.gtosoft.voyager"/>

    <service android:name=".VoyagerService"/>

    <activity android:name=".SettingsActivity" 
            android:label="Voyager"
            android:configChanges="keyboardHidden|orientation">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【问题讨论】:

  • 很确定它是您所追求的完全限定名称空间的字符串。
  • 这是有道理的,但是字符串必须是什么? com.gtosoft.voyager.????

标签: android service android-intent


【解决方案1】:

虽然 CommonsWare 的回答在 2010 年 4 月是正确的,但情况发生了变化,您需要实际 setComponent 才能在另一个应用程序中启动服务。例如,

com.xyz.app1 中的活动:

Intent i = new Intent();
String pkg = "com.xyz.app2";
String cls = "com.xyz.app2.MyService";
i.setComponent(new ComponentName(pkg, cls));
startService(i);

【讨论】:

    【解决方案2】:

    我会在服务上设置一个&lt;intent-filter&gt;,并使用自定义操作,然后在您的Intent 中使用它来启动或绑定到该服务。您可以在这对 client 和 service 示例项目中看到一个示例。

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多