如何启动在不同包中定义的服务?

Bra*_*ein 3 service android android-intent

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

我想我需要一个意图,但是在用startService()开始之前我会将arg(s)传递给意图吗?

如果他们在我可以使用的同一个包中

Intent svc=new Intent (SettingsActivity.this,VoyagerService.class);
startService(svc);
Run Code Online (Sandbox Code Playgroud)

清单的片段定义了服务

<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>
Run Code Online (Sandbox Code Playgroud)

wor*_*ked 11

虽然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);
Run Code Online (Sandbox Code Playgroud)


Com*_*are 5

我将<intent-filter>使用自定义操作在服务上设置一个,然后在您的Intent服务中使用它来启动或绑定到该服务。您可以在这对客户端服务示例项目中看到一个示例。