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
- 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/
1 /*Testservice.java*/2 PackageCom.android.server;3 ImportAndroid.content.Context;4 ImportAndroid.os.Handler;5 ImportAndroid.os.ITestService;6 ImportAndroid.os.Looper;7 ImportAndroid.os.Message;8 Importandroid.os.Process;9 ImportAndroid.util.Log;Ten Public classTestserviceextendsItestservice.stub { One Private Static FinalString TAG = "Testservice"; A PrivateTestworkerthread Mworker; - PrivateTestworkerhandler Mhandler; - PrivateContext Mcontext; the PublicTestservice (Context context) { - Super(); -Mcontext =context; -Mworker =NewTestworkerthread ("Testserviceworker"); + Mworker.start (); -LOG.I (TAG, "spawned worker thread"); + } A at Public voidSetValue (intval) { -LOG.I (TAG, "setValue" +val); -Message msg =Message.obtain (); -Msg.what =Testworkerhandler.message_set; -MSG.ARG1 =Val; - mhandler.sendmessage (msg); in } - to Private classTestworkerthreadextendsThread { + PublicTestworkerthread (String name) { - Super(name); the } * Public voidrun () { $ Looper.prepare ();Panax NotoginsengMhandler =NewTestworkerhandler (); - Looper.loop (); the } + } A the Private classTestworkerhandlerextendsHandler { + Private Static Final intMessage_set = 0; - @Override $ Public voidhandlemessage (Message msg) { $ Try { - if(Msg.what = =Message_set) { -LOG.I (TAG, "set message Received:" +msg.arg1); the } -}Catch(Exception e) {Wuyi //Log, don ' t crash! theLOG.E (TAG, "Exception in Testworkerhandler.handlemessage:", e); - } Wu } - } About}
Register Service
- Register Service in Systemserver.java
/**/try {"Test Service"); New catch (Throwable e) { "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 WHI CH is exposed by service* /package Android.os; Interface Itestservice {/*** {@hide} */ void setValue (int val);}
Add [service].aidl for Build
/**/... 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; Public classHelloServerextendsActivity {Private Static FinalString DTAG = "HelloServer"; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Itestservice om= ITestService.Stub.asInterface (Servicemanager.getservice ("Test")); Try{log.d (DTAG,"Going to call service"); Om.setvalue (20); LOG.D (DTAG,"Service called succesfully"); } Catch(Exception e) {log.d (DTAG,"FAILED to call service"); E.printstacktrace (); } }}
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
(From:http://processors.wiki.ti.com/index.php/android-adding_systemservice)