1.frameworks/base 目錄下添加對應的文件清單如下
frameworks/base/core/java/android/app/HelloWorldManager.java
frameworks/base/core/java/android/app/IHelloWorldManager.aidl
frameworks/base/services/core/java/com/android/server/HelloWorldService.java
很好理解 一個service 一個manager 一個aidl文件
功能很簡單就打印 Hello,World!
再修改
frameworks/base/core/java/android/app/SystemServiceRegistry.java
frameworks/base/core/java/android/content/Context.java
frameworks/base/services/java/com/android/server/SystemServer.java
很顯然是爲了添加及註冊系統級service "hello"
注意修改完成後使用make update-api
特別提醒如果此步驟中 make update-api 不成功一定要修改成編譯成功
make update-api 成功後會修改幾個txt文件
frameworks/base/Android.mk
frameworks/base/api/current.txt
frameworks/base/api/system-current.txt
frameworks/base/api/test-current.txt
記得在修改SystemServiceRegistry.java Context.java SystemServer.java 參考Vibrator添加
SystemServiceRegistry.java
registerService(Context.HELLO_SERVICE, HelloWorldManager.class,
new CachedServiceFetcher<HelloWorldManager>() {
@Override
public HelloWorldManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder binder;
if (true){//ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
binder = ServiceManager.getServiceOrThrow(Context.HELLO_SERVICE);
} else {
binder = ServiceManager.getService(Context.HELLO_SERVICE);
}
return new HelloWorldManager(ctx,IHelloWorldManager.Stub.asInterface(binder));
}});
a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/
old mode 100644
new mode 100755
index df351ba..584a8c4
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -2911,6 +2911,7 @@ public abstract class Context {
ACCOUNT_SERVICE,
ACTIVITY_SERVICE,
ALARM_SERVICE,
+ HELLO_SERVICE,
NOTIFICATION_SERVICE,
ACCESSIBILITY_SERVICE,
CAPTIONING_SERVICE,
@@ -3018,6 +3019,9 @@ public abstract class Context {
* <dt> {@link #ALARM_SERVICE} ("alarm")
* <dd> A {@link android.app.AlarmManager} for receiving intents at the
* time of your choosing.
+ * <dt> {@link #HELLO_SERVICE} ("hello")
+ * <dd> A {@link android.app.HelloWorldManager} for receiving intents at the
+ * time of your choosing.
* <dt> {@link #NOTIFICATION_SERVICE} ("notification")
* <dd> A {@link android.app.NotificationManager} for informing the user
* of background events.
@@ -3082,6 +3086,8 @@ public abstract class Context {
* @see android.os.PowerManager
* @see #ALARM_SERVICE
* @see android.app.AlarmManager
+ * @see #HELLO_SERVICE
+ * @see android.app.HelloWorldManager
* @see #NOTIFICATION_SERVICE
* @see android.app.NotificationManager
* @see #KEYGUARD_SERVICE
@@ -3133,7 +3139,8 @@ public abstract class Context {
* Currently available classes are:
* {@link android.view.WindowManager}, {@link android.view.LayoutInflater},
* {@link android.app.ActivityManager}, {@link android.os.PowerManager},
- * {@link android.app.AlarmManager}, {@link android.app.NotificationManager},
+ * {@link android.app.AlarmManager},
+ * {@link android.app.HelloWorldManager}, {@link android.app.NotificationManager},
* {@link android.app.KeyguardManager}, {@link android.location.LocationManager},
* {@link android.app.SearchManager}, {@link android.os.Vibrator},
* {@link android.net.ConnectivityManager},
@@ -3238,6 +3245,16 @@ public abstract class Context {
* @see android.app.AlarmManager
*/
public static final String ALARM_SERVICE = "alarm";
+
+ /**
+ * Use with {@link #getSystemService} to retrieve a
+ * {@link android.app.HelloWorldManager} for receiving intents at a
+ * time of your choosing.
+ *
+ * @see #getSystemService
+ * @see android.app.HelloWorldManager
+ */
+ public static final String HELLO_SERVICE = "hello";
a/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -140,6 +140,7 @@ import static android.view.Display.DEFAULT_DISPLAY;
import java.lang.reflect.Constructor;
import android.os.IInterface;
import java.lang.reflect.InvocationTargetException;
+import com.android.server.HelloWorldService;
//import com.mediatek.fullscreenswitch.FullscreenSwitchService;
/// @}
@@ -175,6 +176,8 @@ public final class SystemServer {
"com.android.server.voiceinteraction.VoiceInteractionManagerService";
private static final String PRINT_MANAGER_SERVICE_CLASS =
"com.android.server.print.PrintManagerService";
+ private static final String Hello_MANAGER_SERVICE_CLASS =
+ "com.android.server.HelloWorldService";
private static final String COMPANION_DEVICE_MANAGER_SERVICE_CLASS =
"com.android.server.companion.CompanionDeviceManagerService";
private static final String USB_SERVICE_CLASS =
@@ -713,6 +716,7 @@ public final class SystemServer {
NsdService serviceDiscovery= null;
WindowManagerService wm = null;
SerialService serial = null;
+ HelloWorldService hello = null;
NetworkTimeUpdateService networkTimeUpdater = null;
CommonTimeManagementService commonTimeMgmtService = null;
InputManagerService inputManager = null;
@@ -1357,6 +1361,19 @@ public final class SystemServer {
traceEnd();
}
+ if (true) {
+ traceBeginAndSlog("StartHelloService");
+ try {
+ // Serial port support
+ hello = new HelloWorldService(context);
+ ServiceManager.addService(Context.HELLO_SERVICE, hello);
+ } catch (Throwable e) {
+ Slog.e(TAG, "Failure starting StartHelloService", e);
+ }
+ traceEnd();
+ }
+
frameworks/base/core/java/android/app/HelloWorldManager.java
package android.app;
import android.os.RemoteException;
import android.annotation.SystemService;
import android.content.Context;
@SystemService(Context.HELLO_SERVICE)
public final class HelloWorldManager{
private final IHelloWorldManager mService;
private Context mContext;
/**
* @hide to prevent subclassing from outside of the framework
*/
HelloWorldManager(Context context,IHelloWorldManager service){
mContext = context;
mService = service;
}
/**
* Like {@link #HelloWorldManager( )}, but allowing the caller to specify
* that the vibration is owned by someone else.
* @hide
*/
public void printHello(){
try{
mService.printHello();
}catch (RemoteException ex){
}
}
}
frameworks/base/core/java/android/app/IHelloWorldManager.aidl
package android.app;
/**
* System private API for talking with the helloworld service.
* {@hide}
*/
interface IHelloWorldManager{
void printHello();
}
frameworks/base/services/core/java/com/android/server/HelloWorldService.java
package com.android.server;
import android.app.IHelloWorldManager;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
import android.content.Context;
import com.android.server.SystemService;
public class HelloWorldService extends IHelloWorldManager.Stub {
private final static String LOG_TAG = "HelloWorldService";
private static final int BACKGROUND_USER_ID = -10;
private final Object mLock = new Object();
private final Context mContext;
HelloWorldService(Context context) {
mContext = context;
}
@Override
public void printHello() throws RemoteException {
Slog.i(LOG_TAG,"xuyong Hello,World!");
}
}
當然不要忘了frameworks/base/Android.mk 中添加aidl core/java/android/app/IHelloWorldManager.aidl \
如果上述操作做完編譯後無誤的話至少hello service現在是在系統中的
在添加te文件時有兩種思路 一種是按照system/sepolicy 目錄中添加vibrator service的步驟添加hello service
另一種是在device 目錄下添加hello.te 文件等操作實現。下面先說簡單的:在system/sepolicy 目錄中添加
system/sepolicy/prebuilts/api/26.0/nonplat_sepolicy.cil
system/sepolicy/prebuilts/api/26.0/private/service_contexts
system/sepolicy/prebuilts/api/26.0/public/service.te
system/sepolicy/private/compat/26.0/26.0.cil
system/sepolicy/private/service_contexts
system/sepolicy/public/service.te
此步驟很簡單 就完整照Vibrator 添加 注意一點 :hello" 對應 Context 中的HELLO_SERVICE
system/sepolicy/private/service_contexts
hello u:object_r:hello_service:s0
system/sepolicy/prebuilts/api/26.0/public/service.te
type hello_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
文件修改後可以mmm system/sepolicy/ 驗證語法或規則是否符合要求。
下面介紹另一種添加te文件方式 我們是以sprd的代碼爲例
device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts
device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te
device/sprd/sharkl2/common/plat_sepolicy/public/service.te
device/sprd/sharkl2/common/sepolicy/system_server.te
device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts
hello u:object_r:hello_service:s0
device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te添加
allow system_server hello_service:service_manager { add };
device/sprd/sharkl2/common/plat_sepolicy/public/service.te添加
type hello_service, service_manager_type;
device/sprd/sharkl2/common/sepolicy/system_server.te添加
allow system_server hello_service:service_manager add;
添加兩個文件
device/sprd/sharkl2/common/plat_sepolicy/private/hello.te
typeattribute hello coredomain;
init_daemon_domain(hello)
device/sprd/sharkl2/common/plat_sepolicy/public/hello.te
type hello, domain;
type hello_exec, exec_type, file_type;
文件修改後可以mmm system/sepolicy/ 驗證語法或規則是否符合要求.
2.完整編譯 刷機 開機後 使用adb shell 進入後 手機 service list | grep hello 如果能顯示說明系統中已添加成功hello service
3.編寫測試程序 使用hello service 我是在Settings 中添加的
private void callHelloService(){
Log.d("xuyong","onclick begin");
HelloWorldManager hello = ((HelloWorldManager)mContext.getSystemService(Context.HELLO_SERVICE));
if (hello != null){
Log.d("xuyong","callHelloService successful!");
hello.printHello();
}
Log.d("xuyong","onclick end");
}
記得import android.app.HelloWorldManager;
最後上傳源碼:
https://download.csdn.net/download/yong_xu/10519366