Android 新增系統串列埠服務步驟
系統服務實際上是呼叫了遠端Bind物件進行操作
1:新增aidl檔案到 framworks/base/core/java/android/hardware/ISerialManager.aidl//這個檔案是需要自己現實的
2:編譯指令碼中新增申明
frameworks/base/Android.mk: core/java/android/hardware/ISerialManager.aidl \
3:Context檔案中新增變數
frameworks/base/core/java/android/content/Context.java: public static final String SERIAL_SERVICE = "serial";
4:新增呼叫遠端服務的檔案
frameworks/base/services/java/com/android/server/ SerialService.java//這個檔案是需要自己現實的
到frameworks/base/services/java/com/android/server/SystemServer.java:
例如:public class SerialService extends ISerialManager.Stub檔案
try {
Slog.i(TAG, "Serial Service");
// Serial port support
serial = new SerialService(context);
ServiceManager.addService(Context.SERIAL_SERVICE, serial);
} catch (Throwable e) {
Slog.e(TAG, "Failure starting SerialService", e);
}
}
5:添加註冊服務到 frameworks/base/core/java/android/app/ContextImpl.java: 獲取SerialManager
import android.hardware.SerialManager;
import android.hardware.ISerialManager;//這兩個檔案是需要自己現實的
構造方法中傳入兩個引數 public SerialManager(Context context, ISerialManager service) {
mContext = context;
mService = service;
}
registerService(SERIAL_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(SERIAL_SERVICE);
return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
}});
接下來就可以通過(SerialManager)getSystemService(Context.SERIAL_SERVICE);來獲取SerialManager