【发布时间】:2023-03-28 03:21:01
【问题描述】:
我正在使用 am startservice 从 shell 运行 android 应用程序的服务。一切正常。
am startservice -a com.anotherdeveloper.app.SERVICE --ef a 1 --ef b 2
我想知道是否可以在不作为开发人员的情况下启动另一个 android 应用程序的服务? 根据此链接,有可能: How to start android service from another Android app
但我不太明白该怎么做。
这是我在MainActivity 中使用的代码:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.anotherdeveloper.app", "com.anotherdeveloper.app.SERVICE"));
ComponentName c = getApplicationContext().startForegroundService(intent);
我在AndroidManifest.xml 中添加了这个:
<service
android:name="com.anotherdeveloper.app.SERVICE"
android:enabled="true"
android:exported="true" />
但它在清单中显示一个错误,因为 Android Studio 无法找到我手机上安装的此应用程序的包名称。
更新
感谢@Martin Marconcini,这是我正在使用的代码:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.anotherdeveloper.app","com.anotherdeveloper.app.SERVICE"));
intent.putExtra("cty", 4);
intent.putExtra("stt", 2);
startService(intent);
这是AndroidManifest.xml中的服务:
<service name="com.anotherdeveloper.app.General">
<intent-filter>
<action name="com.anotherdeveloper.app.SERVICE"/>
</intent-filter>
<intent-filter>
<action name="com.anotherdeveloper.app.SERVICE_ALT"/>
</intent-filter>
<intent-filter>
<action name="com.anotherdeveloper.app.TERMINATE"/>
</intent-filter>
</service>
【问题讨论】:
标签: java android android-studio android-intent android-service