首先在frameworks/base/下创建如下目录mcserial/java/com/mc/serial 然后在此目录下添加如下三个文件IMcSerialNumService.aidl McSerialNumManager.java McSerialNumService.java ,三个文件内容如下。首先是IMcSerialNumService.aidl 文件,具体内容如下
package com.mc.serial;
interface IMcSerialNumService {
boolean setSerialNum(String value);
}
主要是增加接口,接着McSerialNumManager.java文件,这里也是接口,主要实现是在服务里面。
package com.mc.serial;
import android.util.Log;
import android.content.Context;
import android.os.RemoteException;
public class McSerialNumManager {
private static final String TAG = "McSerialManager";
private IMcSerialNumService mService;
private Context mContext;
public McSerialNumManager(Context context, IMcSerialNumService service) {
mContext = context;
mService = service;
}
public boolean setSerialNum(String value) {
try {
return mService.setSerialNum(value);
} catch (RemoteException e) {
Log.e(TAG, "Service not running.");
return false;
}
}
}
接着就是服务McSerialNumService.java 文件了。
package com.mc.serial;
import android.util.Log;
import android.os.SystemProperties;
import android.content.Context;
public class McSerialNumService extends IMcSerialNumService.Stub {
public static final String TAG = "zxy McSerialService";
private Context mContext;
public McSerialNumService(Context context) {
mContext = context;
}
public boolean setSerialNum(String value){
Log.d(TAG,"setSerialNum = " + value);
SystemProperties.set("ro.serialno", value);
String mSerialNum = SystemProperties.get("ro.serialno",null);
Log.d(TAG,"get setSerialNum = " + mSerialNum);
if (mSerialNum.equals(value) ) return true;
else return false;
}
}
这里为具体实现的功能。至此,一个接口就添加完成了。然后添加到白名单中,我这里的文件是这里,这个需要根据sdk去修改对应的文件。
--- a/android/build/make/core/tasks/check_boot_jars/package_whitelist.txt
+++ b/android/build/make/core/tasks/check_boot_jars/package_whitelist.txt
@@ -263,3 +263,5 @@ com\.mc\.android\.mchardwarestatusmanager
+com\.mc\.serial
接着修改Android.bp ,让我们编写的文件能参与编译
diff --git a/android/frameworks/base/Android.bp b/android/frameworks/base/Android.bp
index 9b3e1dd578..53ddbfe5d5 100644
--- a/android/frameworks/base/Android.bp
+++ b/android/frameworks/base/Android.bp
@@ -75,6 +75,8 @@ java_defaults {
"wifi/java/**/*.java",
"keystore/java/**/*.java",
"rs/java/**/*.java",
+ "mcserial/java/**/*.java",
+ "mcserial/java/com/mc/serial/IMcSerialNumService.aidl",
":framework-javastream-protos",
接着添加我们的服务名称。
diff --git a/android/frameworks/base/core/java/android/content/Context.java b/android/frameworks/base/core/java/android/content/Context.java
index 3be4ed4905..aefc5f6802 100644
--- a/android/frameworks/base/core/java/android/content/Context.java
+++ b/android/frameworks/base/core/java/android/content/Context.java
@@ -3825,6 +3825,13 @@ public abstract class Context {
*/
@TestApi public static final String TEST_NETWORK_SERVICE = "test_network";
+
+ /**
+ * @see
+ * @see com.mc.android.McFirmwareInfoManager
+ */
+ public static final String MCSERIALNUM_SERVICE = "mcserialnum";
+
/**
* @see
* @see com.mc.android.McFirmwareInfoManager
然后就是注册服务了。
diff --git a/android/frameworks/base/core/java/android/app/SystemServiceRegistry.java b/android/frameworks/base/core/java/android/app/SystemServiceRegistry.java
index 1b687d860c..72cb7b2cab 100644
--- a/android/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/android/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -204,6 +204,10 @@ import com.mc.android.mcsecure.McSecureService;
import com.mc.android.mcsecure.McSecureManager;
import com.mc.android.mcbootanimation.McBootanimationManager;
import com.mc.android.mcbootanimation.IMcBootanimationService;
+
+import com.mc.serial.McSerialNumManager;
+import com.mc.serial.IMcSerialNumService;
+
import com.mc.android.mctime.McTimeManager;
import com.mc.android.mctime.IMcTimeService;
import com.mc.android.mchardwarestatusmanager.McHardwareStatusManager;
@@ -776,6 +780,14 @@ final class SystemServiceRegistry {
return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
}});
+ registerService(Context.MCSERIALNUM_SERVICE, McSerialNumManager.class,
+ new CachedServiceFetcher<McSerialNumManager>() {
+ @Override
+ public McSerialNumManager createService(ContextImpl ctx) {
+ IBinder b = ServiceManager.getService(Context.MCSERIALNUM_SERVICE);
+ return new McSerialNumManager(ctx, IMcSerialNumService.Stub.asInterface(b));
+ }});
+
registerService(Context.MCFIRMWAREINFO_SERVICE, McFirmwareInfoManager.class,
new CachedServiceFetcher<McFirmwareInfoManager>() {
@Override
这里是添加服务。
diff --git a/android/frameworks/base/services/java/com/android/server/SystemServer.java b/android/frameworks/base/services/java/com/android/server/SystemServer.java
index 097ec808e7..07d36ad7bd 100644
--- a/android/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/android/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -186,6 +186,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import dalvik.system.DexClassLoader;
+import com.mc.serial.McSerialNumService;
+
import com.mc.android.mcfirmwareinfo.McFirmwareInfoService;
import com.mc.android.mcsystemui.McSystemUiService;
@@ -949,6 +951,7 @@ public final class SystemServer {
NsdService serviceDiscovery = null;
WindowManagerService wm = null;
SerialService serial = null;
+ McSerialNumService mcSerialNumservice = null;
McFirmwareInfoService mcFirmwareInfoMS = null;
McHomeManagerService mcHomeManagerService = null;
McWatchdogService mcWatchdogMS = null;
@@ -1693,6 +1696,14 @@ public final class SystemServer {
Slog.e(TAG, "Failure starting McEthernetService", e);
}
+ try {
+ Slog.i(TAG, "McSerialNumService Service");
+ mcSerialNumservice = new McSerialNumService(context);
+ ServiceManager.addService(Context.MCSERIALNUM_SERVICE, mcSerialNumservice);
+ } catch (Throwable e) {
+ Slog.e(TAG, "Failure starting McFirmwareInfoService", e);
+ }
+
至此所有都添加完成了。添加完成后先执行make update-api 命令,然后frameworks/base/api/current.txt 会更新。
diff --git a/android/frameworks/base/api/current.txt b/android/frameworks/base/api/current.txt
index 6b9d0929f1..02137315c0 100755
--- a/android/frameworks/base/api/current.txt
+++ b/android/frameworks/base/api/current.txt
@@ -9842,6 +9842,7 @@ package android.content {
field public static final String MCETHTETHER_SERVICE = "mcethtether";
field public static final String MCFIRMWAREINFO_SERVICE = "mcfirmwareinfo";
field public static final String MCNETCOEXIST_SERVICE = "mcnetcoexist";
+ field public static final String MCSERIALNUM_SERVICE = "mcserialnum";
field public static final String MCSOUND_SERVICE = "mcsound";
field public static final String MCWATCHDOG_SERVICE = "mcwatchdog";
field public static final String MC_BOOTANIMATION_MANAGER = "mcBootanimationManager";
@@ -58058,6 +58059,29 @@ package com.mc.android.mctime {
}
+package com.mc.serial {
+
+ public interface IMcSerialNumService extends android.os.IInterface {
+ method public boolean setSerialNum(String) throws android.os.RemoteException;
+ }
+
+ public static class IMcSerialNumService.Default implements com.mc.serial.IMcSerialNumService {
+ ctor public IMcSerialNumService.Default();
+ method public android.os.IBinder asBinder();
+ method public boolean setSerialNum(String) throws android.os.RemoteException;
+ }
+
+ public abstract static class IMcSerialNumService.Stub extends android.os.Binder implements com.mc.serial.IMcSerialNumService {
+ ctor public IMcSerialNumService.Stub();
+ method public android.os.IBinder asBinder();
+ method public static com.mc.serial.IMcSerialNumService asInterface(android.os.IBinder);
+ method public static com.mc.serial.IMcSerialNumService getDefaultImpl();
+ method public boolean onTransact(int, android.os.Parcel, android.os.Parcel, int) throws android.os.RemoteException;
+ method public static boolean setDefaultImpl(com.mc.serial.IMcSerialNumService);
+ }
+
+}
+
然后编译安卓即可。接着就是添加测试了,首先使用AS创建个新工程。然后如下图所示创建个新module , 选择如下所示选项,然后点击finish 即可。 然后工程就多了如下图所示module。 接着在此module添加IMcSerialNumService.aidl 和McSerialNumManager.java 文件,如下图所示。 这里IMcSerialNumService.aidl 文件内容同之前在源码中添加的相同即可。 接着修改McSerialNumManager.java 文件,内容如下。 这里只保留了方法即可。然后点击这个即可实现编译。 等待编译完成,产生如下图所示aar包即可。
|