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.

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 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

Android-Adding SystemService_第1张图片

Create service

  • Add your code to frameworks/base/services/java/com/android/server/
 1 /*TestService.java */ 2 package com.android.server; 3 import android.content.Context; 4 import android.os.Handler; 5 import android.os.ITestService; 6 import android.os.Looper; 7 import android.os.Message; 8 import android.os.Process; 9 import android.util.Log;10 public class TestService extends ITestService.Stub {11     private static final String TAG = "TestService";12     private TestWorkerThread mWorker;13     private TestWorkerHandler mHandler;14     private Context mContext;15     public TestService(Context context) {16         super();17         mContext = context;18         mWorker = new TestWorkerThread("TestServiceWorker");19         mWorker.start();20         Log.i(TAG, "Spawned worker thread");21     }22  23     public void setValue(int val) {24         Log.i(TAG, "setValue " + val);25         Message msg = Message.obtain();26         msg.what = TestWorkerHandler.MESSAGE_SET;27         msg.arg1 = val;28         mHandler.sendMessage(msg);29     }30  31     private class TestWorkerThread extends Thread {32         public TestWorkerThread(String name) {33             super(name);34         }35         public void run() {36             Looper.prepare();37             mHandler = new TestWorkerHandler();38             Looper.loop();39         }40     }41  42     private class TestWorkerHandler extends Handler {43         private static final int MESSAGE_SET = 0;44         @Override45         public void handleMessage(Message msg) {46             try {47                 if (msg.what == MESSAGE_SET) {48                     Log.i(TAG, "set message received: " + msg.arg1);49                 }50             } catch (Exception e) {51                 // Log, don't crash!52                 Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);53             }54         }55     }56 }

Register service

  • Register service in SystemServer.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);}

Expose service

  • 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

/* * 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

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 */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();        }    }}

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)

更多相关文章

  1. android传递图片和图片与byte转换
  2. android调用系统摄像头拍照图片和视频
  3. android数据库中存取图片
  4. 【TabHost】Android设置TabHost文字及图片
  5. 图片url地址转换成file文件
  6. android下载网络图片并缓存
  7. Android 通过Matrix来对图片进行缩放,旋转和平移
  8. android实现图片上传功能(springMvc)

随机推荐

  1. Android:Deprecated Thread methods are
  2. Android(安卓)获得手机屏幕大小
  3. Android_ScrollView
  4. android 跳动频谱 播放器(类似千千静听)
  5. android studio使用android annotations
  6. Android应用程序编译问题
  7. Timer的schedule和scheduleAtFixedRate的
  8. SeekBar 粗略小节
  9. 第一个android程序以及遇到问题解决办法
  10. Android-->RatingBar自定义大小,自定义样