NOTICE: The Processors Wiki will End-of-Life in December of 2020. It is recommended to download any files or other content you may need that are hosted on processors.wiki.ti.com. The site is now set to read only.

Android-Adding SystemService

From Texas Instruments Wiki
Jump to: navigation, search

Content is no longer maintained and is being kept for reference only!

This wiki page will demonstrate - "How to add system service to android framework". Example - "Adding a Bluetooth HID service" - taken as reference of understanding.This will also help to add support for more bluetooth profiles into android framework.

What is service?[edit]

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 provide a user interface. Another application component can start a service and it will 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 provider, all from the background.

Service layer[edit]

Customservice.png

Create service[edit]

  • Add your code to frameworks/base/services/java/com/android/server/

<syntaxhighlight lang='java'> /*TestService.java */ package com.android.server; import android.content.Context; import android.os.Handler; import android.os.ITestService; import android.os.Looper; import android.os.Message; import android.os.Process; import android.util.Log; public class TestService extends ITestService.Stub {

   private static final String TAG = "TestService";
   private TestWorkerThread mWorker;
   private TestWorkerHandler mHandler;
   private Context mContext;
   public TestService(Context context) {
       super();
       mContext = context;
       mWorker = new TestWorkerThread("TestServiceWorker");
       mWorker.start();
       Log.i(TAG, "Spawned worker thread");
   }
   public void setValue(int val) {
       Log.i(TAG, "setValue " + val);
       Message msg = Message.obtain();
       msg.what = TestWorkerHandler.MESSAGE_SET;
       msg.arg1 = val;
       mHandler.sendMessage(msg);
   }
   private class TestWorkerThread extends Thread {
       public TestWorkerThread(String name) {
           super(name);
       }
       public void run() {
           Looper.prepare();
           mHandler = new TestWorkerHandler();
           Looper.loop();
       }
   }
   private class TestWorkerHandler extends Handler {
       private static final int MESSAGE_SET = 0;
       @Override
       public void handleMessage(Message msg) {
           try {
               if (msg.what == MESSAGE_SET) {
                   Log.i(TAG, "set message received: " + msg.arg1);
               }
           } catch (Exception e) {
               // Log, don't crash!
               Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);
           }
       }
   }

} </syntaxhighlight>

Register service[edit]

  • Register service in SystemServer.java

<syntaxhighlight lang='java'>

/*

* go to function "@Override public void run()"
* ........
* Add following block after line "if (factoryTest != SystemServer.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);

} </syntaxhighlight>

Expose service[edit]

  • A service can expose set of functions that can be access by other process/application. Exposed functions are required to be 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 are exposed by service
*/
package android.os;
interface ITestService {
/**
* {@hide}
*/
	void setValue(int val);
}

Add [service].aidl for build[edit]

/*
 * 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.Service is now ready to use by other application/process.

Using service[edit]

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.

<syntaxhighlight lang='java'> /*

* HelloServer.java
*/

package com.Test.helloserver; import android.app.Activity; import android.os.Bundle; import android.os.ServiceManager; import android.os.ITestService; import android.util.Log; public class HelloServer extends Activity {

   private static final String DTAG = "HelloServer";
   /** Called when the activity is first created. */
   @Override
   public void onCreate(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();
       }
   }

} </syntaxhighlight>

References[edit]

Support[edit]

For community support join http://groups.google.com/group/rowboat
For IRC #rowboat on irc.freenode.net

E2e.jpg {{
  1. switchcategory:MultiCore=
  • For technical support on MultiCore devices, please post your questions in the C6000 MultiCore Forum
  • For questions related to the BIOS MultiCore SDK (MCSDK), please use the BIOS Forum

Please post only comments related to the article Android-Adding SystemService here.

Keystone=
  • For technical support on MultiCore devices, please post your questions in the C6000 MultiCore Forum
  • For questions related to the BIOS MultiCore SDK (MCSDK), please use the BIOS Forum

Please post only comments related to the article Android-Adding SystemService here.

C2000=For technical support on the C2000 please post your questions on The C2000 Forum. Please post only comments about the article Android-Adding SystemService here. DaVinci=For technical support on DaVincoplease post your questions on The DaVinci Forum. Please post only comments about the article Android-Adding SystemService here. MSP430=For technical support on MSP430 please post your questions on The MSP430 Forum. Please post only comments about the article Android-Adding SystemService here. OMAP35x=For technical support on OMAP please post your questions on The OMAP Forum. Please post only comments about the article Android-Adding SystemService here. OMAPL1=For technical support on OMAP please post your questions on The OMAP Forum. Please post only comments about the article Android-Adding SystemService here. MAVRK=For technical support on MAVRK please post your questions on The MAVRK Toolbox Forum. Please post only comments about the article Android-Adding SystemService here. For technical support please post your questions at http://e2e.ti.com. Please post only comments about the article Android-Adding SystemService here.

}}

Hyperlink blue.png Links

Amplifiers & Linear
Audio
Broadband RF/IF & Digital Radio
Clocks & Timers
Data Converters

DLP & MEMS
High-Reliability
Interface
Logic
Power Management

Processors

Switches & Multiplexers
Temperature Sensors & Control ICs
Wireless Connectivity