編輯:初級開發
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
以Windows平台的SDK為例,這裡Android開發網的模擬器配置路徑為 C:\Users\Administrator\.android\avd\2.3.3.avd
我首先從宏觀的角度觀察Binder,Service,Service Manager,並闡述各自的概念。從Linux的概念空間中,android的設計Activity托管
1.下載需要用到的工具: (1)下載Java的IDE開發工具? Eclipse 到Eclipse官方網站下載Ecplise For Java EE的最新Windo
圖2:OPhone的水平分工體制通信運營商中國移動占據主導。軟件開發和終端開發由播思通訊和各終端廠商負責。日本的窗口為鵬開科技。(圖:播思通訊的圖片。佐佐木陽拍攝,中國