From Http://processors.wiki.ti.com/index.php/Android-Adding_SystemService
android-adding Systemservice
This wiki page would demonstrate-"how to add the system service to the Android framework". Example-"Adding a Bluetooth HID service"-taken as reference of understanding. This would also help to add support for more Bluetooth profiles into the Android framework.
Contents[Hide]
- 1 What is service?
- 2 Service Layer
- 3 Create Service
- 4 Register Service
- 5 Expose Service
- 6 Add [service].aidl for Build
- 7 Using Service
- 8 References
- 9 Support
What is service?
As per the definition given at http://developer.android.com/guide/topics/fundamentals/services.html
A Service is an application component that can perform long-running operations in the background and does not pro Vide a user interface. Another application component can start a service and it would continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provide R, all from the background.
Service Layer
Create Service
- ADD your code to frameworks/base/services/java/com/android/server/
/*testservice.java * *PackageCom.android.server;ImportAndroid.content.Context;ImportAndroid.os.Handler;ImportAndroid.os.ITestService;ImportAndroid.os.Looper;ImportAndroid.os.Message;ImportAndroid.os.Process;ImportAndroid.util.Log;PublicClass TestserviceExtends Itestservice.Stub{PrivateStaticFinalString TAG="Testservice";Private Testworkerthread Mworker;Private Testworkerhandler Mhandler;PrivateContext Mcontext;Public Testservice(Context context){Super(); Mcontext= Context; Mworker=New Testworkerthread("Testserviceworker"); Mworker.Start(); Log.I(TAG,"spawned worker thread");}Publicvoid SetValue(int Val){Log.I(TAG,"SetValue"+ Val); Message msg= Message.Obtain(); Msg.What= Testworkerhandler.Message_set; Msg.Arg1= Val; Mhandler.SendMessage(msg);}PrivateClass TestworkerthreadExtendsThread{Public Testworkerthread(String Name){Super(Name);}Publicvoid run(){Looper.Prepare(); Mhandler=New Testworkerhandler(); Looper.Loop();}}PrivateClass TestworkerhandlerExtends Handler{PrivateStaticFinalint Message_set=0; @OverridePublicvoid Handlemessage(Message msg){Try{if (Msg.what == Message_set {log. I (TAG, "set Message Received:" + msg.arg1< Span class= "Br0") } catch (exception e< Span class= "Br0" >) {//Log, don ' t crash! Log. e (TAG, "Exception in Testworkerhandler.handlemessage:", e< Span class= "Br0") } }} /span> Register Service
- Register Service in Systemserver.java
/* * Go to function ' @Override public void Run () ' * ... * Add following block after line "if (factorytest! = Systemse RVer. Factory_test_low_level) {"* *try {Slog. I(TAG, "Test Service"); ServiceManager. addService("Test", new Testservice(context)); catch (throwable e) {Slog. E(TAG, "Failure starting Testservice Service", e);} Expose Service
- A service can expose set of functions that can is access by other process/application. Exposed functions is required to being declared in. aidl file at following location
Frameworks/base/core/java/android/os/[server].aidl
/** aidl file:frameworks/base/core/java/android/os/itestservice.aidl* This file contains definitions of functions which is exposed by Service*/package android.os;interface Itestservice {/*** {@hide}*/void setValue (int val);}
Add [service].aidl for Build
/* * Open frameworks/base/android.mk and add following line */...core/java/android/os/ipowermanager.aidl core/java/ Android/os/itestservice.aidl core/java/android/os/iremotecallback.aidl ...
- Rebuild the Framework/base or Android system. The Service is now a ready-to-use by other application/process.
Using Service
To use service
- First get service handle using "Servicemanager.getservice ()" API
- Use service handle to call set of functions exposed by service
Below is the sample code to use service.
/* * Helloserver.java * *PackageCom. Test.helloserver;ImportAndroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.ServiceManager;ImportAndroid.os.ITestService;ImportAndroid.util.Log;PublicClass HelloServerExtends Activity{PrivateStaticFinalString DTAG="HelloServer";/** called when the activity is first created. */@OverridePublicvoid OnCreate(Bundle savedinstancestate){Super.OnCreate(savedinstancestate); Setcontentview(R.Layout.Main); Itestservice om= Itestservice.Stub.Asinterface(ServiceManager.GetService("Test"));Try{Log.D "going to call service" Om.setvalue (20d (DTAG, "Service called succesfully" ; catch (exception e{log. D "FAILED to call service" E.printstacktrace ( }} /span> References
- Http://developer.android.com/reference/android/app/Service.html
- Http://developer.android.com/guide/topics/fundamentals/services.html
- Http://www.opersys.com/blog/esc-india-2011-wrapup
Support
For community support Join Http://groups.google.com/group/rowboat
For IRC #rowboat on irc.freenode.net
"Quote" Android-adding systemservice