android - 我们可以在android中开发一个可以被任何其他应用程序调用的服务吗?
问题描述
我们可以在应用程序中开发服务,该应用程序本身可以调用该服务。是否可以在不实际运行包含该服务的父应用程序的情况下从任何其他应用程序调用该服务?
如果提供一些代码示例,那就太好了。
解决方案
您应该能够像这样启动您的服务:
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"/>
推荐阅读
- android - Flutter Virgil E3 Kit 不在发布模式下工作,但在调试中工作
- amazon-web-services - 如何在 AWS Lightsail 上自动复制块存储
- java - Java - 围绕 for 循环进行迭代以创建游戏玩家并设置他们的名字
- reactjs - 如何使用来自 NextJS 路由器的 props 衍生组件渲染组件
- angular - 调用 API 后 Angular 不会重新渲染 html 组件
- c++ - 尝试/捕获 C++ 程序中的所有块不起作用
- python - 为 sort() 创建自己的密钥
- azure-active-directory - Azure Active Directory 中的角色和权限
- c# - Xamarin StartTimer 从 TimeSpan 中减去秒数不起作用
- python - 在python中获取硒数据并附加到字典{key:value}对中