Android添加一个系统service
?
Android添加一个系统service
?
Specifying the interface.
This example uses aidl, so the first step is to add aninterface 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 buildsystem:
frameworks/base/Android.mk
Add the following around line 165 (the end of the list ofSRC_FILES):
core/java/android/os/IEneaService.aidl
Implementing the server
The service spawns a worker thread that will do all thework, as part of the system server process. Since the service is created by thesystem server, it will need to be located somewhere where the system server canfind 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 = newEneaWorkerThread("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;
}
} catch (Exception e) {
// Log, don't crash!
Log.e(TAG, "Exception inEneaWorkerHandler.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, newEneaService(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
?