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(安卓)Studio导入项目常出现的问题
  2. 把TextView中的文字添加阴影效果及Style的使用
  3. 动态添加TableRow
  4. Android中设置activity的background为Wallpaper
  5. Android(安卓)Studio导入第三方类库的方法
  6. (已解决)Android(安卓)Studio JVM报错解决方案
  7. Android中TextView中加图片,超链接,部分字或者背景变色。。。不断
  8. Android(安卓)给TextView添加点击事件
  9. Android给TextView添加点击事件的实现方法

随机推荐

  1. Android(安卓)Tab 选项卡的简单实现
  2. 漫谈Android安全框架
  3. 使用adb工具访问sqlite数据库
  4. android 应用程序数据共享shareuserid篇+
  5. android 打开/关闭 移动网络
  6. android studio 在xp/win8上安装,亲测
  7. TextView的XML属性说明全析 ---Android基
  8. Android(安卓)Environment.getExternalSt
  9. Android(安卓)ui utils-简单实用的Androi
  10. Android的Activity跳转动画集合