在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();

}

特别鸣谢:https://blog.csdn.net/sgzy001/article/details/54344739?utm_source=copy

发表评论

CAPTCHAis initialing...