环境
基于rk3566 android 11 系统
步骤
添加aidl文件
frameworks/base/core/java/android/os/IMyTestService.aidl
package android.os;
interface IMyTestService {
void test();
}
添加service
frameworks/base/services/core/java/com/android/server/MyTestService.java
package com.android.server;
import android.util.Log;
import android.os.IMyTestService;
public class MyTestService extends IMyTestService.Stub {
private static final String TAG = "MyTestService";
public MyTestService() {}
@Override
public void test(){
Log.i(TAG,"MyTestService test ...");
}
}
将自定义的service添加到系统中
- 首先在Context中添加常量
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -3494,6 +3494,7 @@ public abstract class Context {
PERMISSION_SERVICE,
LIGHTS_SERVICE,
+ MY_TEST_SERVICE,
})
@Retention(RetentionPolicy.SOURCE)
public @interface ServiceName {}
@@ -5016,6 +5017,13 @@ public abstract class Context {
*/
public static final String TIME_ZONE_RULES_MANAGER_SERVICE = "timezone";
+
+
+ public static final String MY_TEST_SERVICE="my_test";
+
- 在 frameworks/base/services/java/com/android/server/SystemServer.java文件中的startOtherServices方法中加上我们自己的服务
+ try {
+ ServiceManager.addService(Context.MY_TEST_SERVICE, new MyTestService());
+ } catch (Throwable e) {
+ Slog.e(TAG, "Failure starting MyTest Service", e);
+ }
+
添加manager
frameworks/base/core/java/android/app/MyTestManager.java
package android.app;
import android.annotation.NonNull;
import android.content.Context;
import android.os.IMyTestService;
import android.os.RemoteException;
public class MyTestManager {
private IMyTestService mService;
public MyTestManager(@NonNull Context mContext, @NonNull IMyTestService service) {
mService = service;
}
public void test() {
try {
mService.test();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
将manager注册到系统
frameworks/base/core/java/android/app/SystemServiceRegistry.java
diff --git a/frameworks/base/core/java/android/app/SystemServiceRegistry.java b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
index e599a5c..243790d 100644
--- a/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -43,6 +43,8 @@ import android.app.usage.IUsageStatsManager;
import android.app.usage.NetworkStatsManager;
import android.app.usage.StorageStatsManager;
import android.app.usage.UsageStatsManager;
+import android.os.IMyTestService;
+import android.app.MyTestManager;
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothManager;
import android.companion.CompanionDeviceManager;
@@ -1334,6 +1336,18 @@ public final class SystemServiceRegistry {
return new DreamManager(ctx);
}});
+
+ registerService(Context.MY_TEST_SERVICE, MyTestManager.class,
+ new CachedServiceFetcher<MyTestManager>() {
+ @Override
+ public MyTestManager createService(ContextImpl ctx)
+ throws ServiceNotFoundException {
+ IBinder b = MyTestManager.getServiceOrThrow(Context.MY_TEST_SERVICE);
+ IMyTestService service = IMyTestService.Stub.asInterface(b);
+ return new MyTestManager(ctx.getOuterContext(),service);
+ }});
+
+
sInitializing = true;
try {
添加到系统中编译
- 在Android 9 之前 是在frameworks/base/Android.mk中添加,后续已经移到了frameworks/base/Android.bp中
像Android 10的时候只需要添加aidl文件即可
java_defaults {
name: "framework-defaults",
installable: true,
srcs: [
"core/java/**/*.java",
...省略代码...
"core/java/android/app/IMyTestService.aidl",
到了Android 11 的时候又变了,我们添加的文件直接不用管,已经被包含进去了
filegroup {
name: "framework-core-sources",
srcs: [
"core/java/**/*.java",
"core/java/**/*.aidl",
],
path: "core/java",
}
编译
build/envsetup.sh
lunch rk3566_r-userdebug //看自己平台配置
好像不需要执行 make update-api 直接使用./build.sh -KUAuo 就能编译出来
导出我们需要的jar包
- 在out/target/common/obj/JAVA_LIBRARIES目录下找到framework-minus-apex_intermediates目录,11之前的好像是framework_intermediates目录 现在改成了这个framework-minus-apex_intermediates
- 使用下面的命令生成jar
cd rk3566-11.0/out/target/common/obj/JAVA_LIBRARIES/framework-minus-apex_intermediates
jar -xvf classes.jar android/app/MyTestManager.class
jar -cvf 3566_smatek.jar android
最后导入jar包使用即可
如果报下面这个错误,则需要导入平台的系统签名
java.lang.NoSuchMethodError: No virtual method test()V in class Landroid/app/MyTestManager; or its super classes (declaration of 'android.app.MyTestManager' appears in /system/framework/framework.jar)
有用请点赞,谢谢观看
|