首页 > 解决方案 > 我们可以在android中开发一个可以被任何其他应用程序调用的服务吗?

问题描述

我们可以在应用程序中开发服务,该应用程序本身可以调用该服务。是否可以在不实际运行包含该服务的父应用程序的情况下从任何其他应用程序调用该服务?

如果提供一些代码示例,那就太好了。

标签: androidandroid-service

解决方案


您应该能够像这样启动您的服务:

Intent i = new Intent();
i.setComponent(new ComponentName("com.xxx.yyy", "com.xxx.yyy.YourService"));
ComponentName c = context.startService(i);

您应该具有以下 applicationId :

defaultConfig {
     applicationId "com.xxx.yyy"
}

您需要在清单中定义它,如下所示:

<service android:name="com.xxx.yyy.YourService"
         android:exported="true"/>

推荐阅读