我正在使用am startservice
从外壳运行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当然找不到我的手机上安装的该应用的程序包名称。
简短的回答:这完全取决于第三方的决定(服务创建者在相应的android:exported
中写了什么]
此处有更多详细信息:https://developer.android.com/guide/topics/manifest/service-element
要通过Intent传递参数,您需要使用“附加”。
如果您这样做:
Intent i = new Intent();
i.putXXX(key, value)
例如,您将获得很多选项(每种类型),而不是XXX:
int value = 1;
i.putExtra("Key", value)
// or
String value = "X"
i.putExtra("Key", value)
// or
boolean value = true;
i.putExtra("Key", value)
它们都起作用(当然,使键具有唯一性)。
或者,您可以传递包含一串的Bundle
。
Bundle b = new Bundle();
b.putString("x", "value");
b.putInt("x", 1);
b.putBoolean("x", true);
Intent i = new Intent();
i.putExtra("theBundle", b);
本质上,这非常类似于Google / Java,而其中的关键是一致性。 ;)((当然,如果您传递了捆绑包,则 receiverParcelable
。因此,这是一个必须可打包的键/值存储。
意图数据大小(额外)的限制为X(不记得它是1mb还是什么),但是您不能仅序列化1 TB的信息并通过意图来扔掉它。记住这一点。