Java層Binder使用(ServiceManager)
阿新 • • 發佈:2019-02-09
跟上篇Binder使用一樣,先通過例子來跟蹤Java層Binder機制。本文參考了Binder In java
在Init程序的init2階段,系統啟動了ServerThread,在ServerThread中會啟動很多用Java實現的系統服務
(frameworks/base/services/java/com/android/server/SystemServer.java)
程式碼
- power = new PowerManagerService();
-
ServiceManager.addService(Context.POWER_SERVICE, power);
- context = ActivityManagerService.main(factoryTest);
- Slog.i(TAG, "Display Manager");
- display = new DisplayManagerService(context, wmHandler, uiHandler);
- ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);
通過ServiceManager的addService註冊為binder 的server端。
我們以PowerManagerService
(frameworks/base/services/java/com/android/server/power/)
PowerManagerService繼承於IPowerManager.stub,而IPowerManager.stub位於
(frameworks/base/core/java/com/android/os/IPowerManager.aidl)
- package android.os;
- import android.os.WorkSource;
- /** @hide */
- interface IPowerManager
- {
-
// WARNING: The first two methods must remain the first two methods because their
- // transaction numbers must not change unless IPowerManager.cpp is also updated.
- void acquireWakeLock(IBinder lock, int flags, String tag, in WorkSource ws);
- void releaseWakeLock(IBinder lock, int flags);
- void updateWakeLockWorkSource(IBinder lock, in WorkSource ws);
- boolean isWakeLockLevelSupported(int level);
- void userActivity(long time, int event, int flags);
- void wakeUp(long time);
- void goToSleep(long time, int reason);
- void nap(long time);
- boolean isScreenOn();
- void reboot(boolean confirm, String reason, boolean wait);
- void shutdown(boolean confirm, boolean wait);
- void crash(String message);
- void setStayOnSetting(int val);
- void setMaximumScreenOffTimeoutFromDeviceAdmin(int timeMs);
- // temporarily overrides the screen brightness settings to allow the user to
- // see the effect of a settings change without applying it immediately
- void setTemporaryScreenBrightnessSettingOverride(int brightness);
- void setTemporaryScreenAutoBrightnessAdjustmentSettingOverride(float adj);
- // sets the attention light (used by phone app only)
- void setAttentionLight(boolean on, int color);
- }
Aidl是android內部程序通訊介面的描述語言,通過編譯我們可以編譯出
- package android.os;
- /** @hide */
- publicinterface IPowerManager extends android.os.IInterface {
- /** Local-side IPC implementation stub class. */
- publicstaticabstractclass Stub extends android.os.Binder implements
- android.os.IPowerManager {
- privatestaticfinal java.lang.String DESCRIPTOR = "android.os.IPowerManager";
- /** Construct the stub at attach it to the interface. */
- public Stub() {
- this.attachInterface(this, DESCRIPTOR);
- }
- /**
- * Cast an IBinder object into an android.os.IPowerManager interface,
- * generating a proxy if needed.
- */
- publicstatic android.os.IPowerManager asInterface(
- android.os.IBinder obj) {
- if ((obj == null)) {
- returnnull;
- }
- android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
- if (((iin != null) && (iin instanceof android.os.IPowerManager))) {
- return ((android.os.IPowerManager) iin);
- }
- returnnew android.os.IPowerManager.Stub.Proxy(obj);
- }
- @Override
- public android.os.IBinder asBinder() {
- returnthis;
- }
- @Override
- publicboolean onTransact(int code, android.os.Parcel data,
- android.os.Parcel reply, int flags)
- throws android.os.RemoteException {
- switch (code) {
- case INTERFACE_TRANSACTION: {
- reply.writeString(DESCRIPTOR);
- returntrue;
- }
- case TRANSACTION_acquireWakeLock: {
- data.enforceInterface(DESCRIPTOR);
- android.os.IBinder _arg0;
- _arg0 = data.readStrongBinder();
- int _arg1;
- _arg1 = data.readInt();
- java.lang.String _arg2;
- _arg2 = data.readString();
- android.os.WorkSource _arg3;
- if ((0 != data.readInt())) {
- _arg3 = android.os.WorkSource.CREATOR
- .createFromParcel(data);
- } else {
- _arg3 = null;
- }
- this.acquireWakeLock(_arg0, _arg1, _arg2, _arg3);
- reply.writeNoException();
- returntrue;
- }
- case TRANSACTION_releaseWakeLock: {
- data.enforceInterface(DESCRIPTOR);
- android.os.IBinder _arg0;