Android 添加一个系统service

 

Specifying the interface.

  This example uses aidl, so the first step is to add an interface definition file:

  frameworks/base/core/java/android/os/IEneaService.aidl

  package android.os;

  interface IEneaService {

  /**

  * {@hide}

  */

  void setValue(int val);

  }

  The interface file will need to be added to the build system:

  frameworks/base/Android.mk

  Add the following around line 165 (the end of the list of SRC_FILES):

  core/java/android/os/IEneaService.aidl

  Implementing the server

  The service spawns a worker thread that will do all the work, as part of the system server process. Since the service is created by the system server, it will need to be located somewhere where the system server can find it.

  frameworks/base/services/java/com/android/server/EneaService.java

  package com.android.server;

  import android.content.Context;

  import android.os.Handler;

  import android.os.IEneaService;

  import android.os.Looper;

  import android.os.Message;

  import android.os.Process;

  import android.util.Log;

  public class EneaService extends IEneaService.Stub {

  private static final String TAG = "EneaService";

  private EneaWorkerThread mWorker;

  private EneaWorkerHandler mHandler;

  private Context mContext;

  public EneaService(Context context) {

  super();

  mContext = context;

  mWorker = new EneaWorkerThread("EneaServiceWorker");

  mWorker.start();

  Log.i(TAG, "Spawned worker thread");

  }

  public void setValue(int val)

  {

  Log.i(TAG, "setValue " + val);

  Message msg = Message.obtain();

  msg.what = EneaWorkerHandler.MESSAGE_SET;

 

 

接上页

  msg.arg1 = val;

  mHandler.sendMessage(msg);

  }

  private class EneaWorkerThread extends Thread{

  public EneaWorkerThread(String name) {

  super(name);

  }

  public void run() {

  Looper.prepare();

  mHandler = new EneaWorkerHandler();

  Looper.loop();

  }

  }

  private class EneaWorkerHandler 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 EneaWorkerHandler.handleMessage:", e);

  }

  }

  }

  }

  Add to the system server

  services/java/com/android/server/SystemServer.java

  try {

  Log.i(TAG, "Enea Service");

  ServiceManager.addService(Context. ENEA_SERVICE, new EneaService(context));

  } catch (Throwable e) {

  Log.e(TAG, "Failure starting Enea Service", e);

  }

  Add a constant value to Context

  ./core/java/android/content/Context.java

  public static final String ENEA_SERVICE = "enea";

  最后

  make update-api

  make

 

更多相关文章

  1. ANDROID Porting系列一、ANDROID编译系统
  2. android 系统源码在线阅读-------值得收藏(Android Platform - Lo
  3. Android Studio 系列(二)使用Android Studio 导入整个android 源码
  4. Android系统版本与API Level对照表
  5. Google Android手机操作系统发展历程[信息图表]
  6. 【译】Android系统简介
  7. JAVA & Android 系统环境变量配置
  8. Android 7.1.2(Android N) Android系统启动流程

随机推荐

  1. Android(安卓)Framework AIDL的使用
  2. 推送sdk (类似百度 推送 ) android mina (
  3. Android(安卓)NDK移植libiconv和libxml2
  4. Eclipse 环境下安装PhoneGap开发插件
  5. android 项目串口通信(serialPort)开发遇到
  6. android 开源 OCR 项目 及手写识别[转]
  7. Android(安卓)Weekly Notes Issue #232
  8. Android新线程中更新主线程UI中的View方
  9. AOSP和Chromium的Android(安卓)WebViewTe
  10. Android游戏开发起步(译文)