如何在Android框架中添加自己的系统服务?
我是android新手,我一直在分析android源代码以了解系统服务是如何实现的。我的问题是我是否能够创建自己的系统服务并将其添加到框架中,以便所有应用程序都能够访问我的服务。 任何评论/代码片段都会有帮助。
提前感谢回复者。
I am new to android and i have been analyzing the android source code to understand how the System services are implemented. My Question is am I able to create my own System Service and add it to the framework so that all the applications should be Able to access my service.
Any comments/Code snippets would be helpful.
Thanks to the Replier in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于系统服务来说,还需要更多。本质上,至少对于 Android 2.3,必须扩展 SystemServer.java,以便在 system_server 启动时实例化新服务。
德州仪器 (TI) 提供了一个很好的示例:
http://processors.wiki.ti.com/index.php /Android-Adding_SystemService
CyanogenMod SystemServer.java 还具有用于动态加载系统服务的代码,如 config.xml 中的数组“config_vendorServices”中定义的(请参阅 core/res/res/values/config.xml),我将其相信可以在供应商或设备目录中覆盖。然而,我们自己还没有尝试使用它,我不知道这是 CyanogenMod 特有的还是 2.3.[45] 共有的。
For a system service, more is needed. Essentially, at least for Android 2.3, the SystemServer.java must be extended so that the new service gets instantiated when the system_server starts.
Texas Instruments has kindly provided a nice example:
http://processors.wiki.ti.com/index.php/Android-Adding_SystemService
The CyanogenMod SystemServer.java has also code for dynamically loading system services as defined in the array "config_vendorServices" in config.xml (see core/res/res/values/config.xml), which I believe can be overwritten in the vendor or device directories. However, we haven't tried to use that ourselves, and I don't know if that is CyanogenMod specific or common to 2.3.[45].
您可以添加自己的服务,但无法添加系统服务,除非您可以修改框架源代码。
系统服务以系统权限运行在system_sever中,这使得这些服务可以访问普通应用程序服务无法访问的系统资源。
这篇文章详细讲解了Android中系统服务是如何实现的.
You can add your own service but there is no way to add system service, unless you can modify the framework source code.
System services are running in system_sever with system privilege, which allow those service can access system resource that normal application service can't.
This article explain in detail how the system service are implemented in Android.
按照以下步骤在 android 框架中编写自己的系统服务。
PS:如果您的服务遇到一些致命异常,设备将软重启,因为您的服务在系统服务下运行。
Follow the below steps for writing your own System Service in android framework.
PS: if your service get some fatal exception, device will soft reboot, as your service is running under system service.
在您的活动中:
In your activity: