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.
Android services are system component which allows performing a longer-running operation while not interacting with the user. Just like Activities , Service runs on Main Thread and has a life cycle but unlike Activity , Services do not have a UI.
You declare a service in your app's Manifest, by adding a <service> element as a child of your <application> element. There's a list of attributes that you can use to control a service's behavior, but as a minimum you'll need to provide the service's name (android:name) and a description (android:description).
Starting a service You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.
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].
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With