在android framework中新增自定義系統服務
新增系統服務需要新增aidl,service,manager檔案,需要修改SystemServer,Context,ContextImpl 下面舉例進行說明,主要新增一個服務,用於獲取系統時間和版本號
1.在frameworks/base/core/java/android/app/下新增aidl檔案
frameworks/base/core/java/android/app/ISystemStatusManager.aidl package android.app; interface ISystemStatusManager { String[] getSystemStatus(); }
2.修改frameworks/base/Android.mk檔案,將aidl加到該檔案中,這樣才可以被編譯到,否則編譯不過
將core/java/android/app/ISystemStatusManager.aidl \加到LOCAL_SRC_FILES後面
3.在frameworks/base/services/core/java/com/android/server/下新增service檔案
frameworks/base/services/core/java/com/android/server/SystemStatusManagerService.java package com.android.server; import android.app.ISystemStatusManager; import android.content.Context; import android.os.Build; import android.util.Slog; import java.text.SimpleDateFormat; import java.util.Date; public class SystemStatusManagerService extends ISystemStatusManager.Stub { private Context mContext; public SystemStatusManagerService(Context context) { mContext = context; Slog.d("SystemStatus", "Date:" + getDateTime() + "\nVersion:" + getVersion()); } @Override public String[] getSystemStatus() { String[] infos = new String[2]; infos[0] = "Date:" + getDateTime(); infos[1] = "Version:" + getVersion(); return infos; } private String getDateTime() { SimpleDateFormat format = new SimpleDateFormat("yyyy-DD-mm HH:mm:s"); Date date = new Date(System.currentTimeMillis()); return format.format(date); } private String getVersion() { return Build.DISPLAY; } }
4.在frameworks/base/core/java/android/app/下新增manager
frameworks/base/core/java/android/app/SystemStatusManager.java package android.app; import android.content.Context; import android.os.RemoteException; import android.util.Slog; public class SystemStatusManager { Context mContext; ISystemStatusManager mService; public SystemStatusManager(Context context, ISystemStatusManager service) { mContext = context; mService = service; } public String[] getSystemStatus() { if (mService != null) { try { return mService.getSystemStatus(); } catch (RemoteException e) { Slog.e("SystemStatus", "RemoteException " + e); return null; } } return null; } } 5.在frameworks/base/core/java/android/content/Context.java中新增service名稱定義
public static final String SYSTEM_STATUS_SERVICE = "system_status_service";
6.在frameworks/base/services/java/com/android/server/SystemServer.java中新增服務
在startOtherServices()方法中新增如下程式碼 try { ServiceManager.addService(Context.SYSTEM_STATUS_SERVICE, new SystemStatusManagerService(context)); } catch (Throwable e) { Slog.e(TAG, "Failure starting System Status Service ", e); }
7.在frameworks/base/core/java/android/app/ContextImpl.java中註冊服務
在static {}中新增如下程式碼 registerService(SYSTEM_STATUS_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(SYSTEM_STATUS_SERVICE); return new SystemStatusManager(ctx, ISystemStatusManager.Stub.asInterface(b)); } });
8.編譯framework.jar和services.jar,push到手機,重啟手機
重啟手機後就會輸出如下log: D/SystemStatus( 910): Date:2017-01-00 00:00:13 D/SystemStatus( 910): Version:xxx
9.如果系統app需要使用該service,可以通過如下方式呼叫
SystemStatusManager mManager = (SystemStatusManager) getSystemService(Context.SYSTEM_STATUS_SERVICE); try { String[] infos = mManager.getSystemStatus(); Log.i("SystemStatus", "SystemStatus Date:" + infos[0] + "\nVersion:" + infos[1]); } catch (Exception e) { e.printStackTrace();
}
--------------------- 作者:默默9518 來源:CSDN 原文:https://blog.csdn.net/sgzy001/article/details/54344739?utm_source=copy