需求,希望在Android ROM中添加相应的LogService服务,保存每次的log,用于处理系统死机问题;
本文档基于rk的5.1.1系统;
基本框架:
5.1.1系统在framework/base/services/java/com/android/server/SystemServer.java文件中
startOtherServices函数在启动启动了core服务后,开始启动其他的SystemService;
添加自定义系统服务,只要在函数中找到相应问题,执行
mSystemServiceManager.startService(LogService.class);
即可;
当然,我们需要在frameworks/base/services/core/java/com/android/server目录下增加我们的LogService类LogService.java
package com.android.server;
class LogService extends SystemService {
public static final String TAG = "LogService";
@Override
public void onStart(){
//startService会调用到此处,编写对应的service服务即可;
}
}
至此,添加自定义系统服务的方法已经描述完毕,具体的实现就依据不同的功能进行即可;
以上仅仅介绍了增加LogService的方法,但是随着学习,发现其实并不完整,以下转载一个完整的流程,转载自:
http://blog.csdn.net/shell812/article/details/49819599
内容:
package android.service.audiotest;
interface IAudioTest {
int init();
// int chooseMainSrc(long src);
int mainChooseChannel(long srcId);
int mainInputGain(long gain);
//...
}
LOCAL_SRC_FILES += \
core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \
core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl \
core/java/android/accounts/IAccountManager.aidl \
core/java/android/service/audiotest/IAudioTest.aidl \
core/java/android/view/accessibility/IAccessibilityInteractionConnection.aidl\
package com.android.server.audiotest;
import android.service.audiotest.IAudioTest;
import android.content.Context;
public class AudioTestService extends IAudioTest.Stub{
private String TAG = "AudioTestService ";
private Context context;
private native int AudioTestInitNative();
private native int AudioTestUninitNative();
private native int AudioTestCtrlNative(int cmd, long arg);
public AudioTestService(Context c){
context = c;
init();
}
private int myAudioTestCtrl(AudioCtrl ac, long arg){
return AudioTestCtrlNative(ac.ordinal(), arg);
}
@Override
public int init(){
int ret = 0;
AudioTestInitNative();
return ret;
}
@Override
public int mainChooseChannel(long srcId){
int ret;
ret=myAudioTestCtrl(AudioCtrl.IOCTL_MAIN_CHANNEL, srcId);
return ret;
}
@Override
public int mainInputGain(long gain){
int ret;
ret= myAudioTestCtrl(AudioCtrl.IOCTL_MAIN_INPUT_GAIN, gain);
return ret;
}
}
//....
AlarmManagerService alarm = null;
AudioTestService audiotest = null;
MountService mountService = null;
//....
Slog.i(TAG, "Alarm Manager");
alarm = new AlarmManagerService(context);
ServiceManager.addService(Context.ALARM_SERVICE, alarm);
audiotest = new AudioTestService(context);
ServiceManager.addService(Context.AUDIOTEST_SERVICE, audiotest);
Slog.i(TAG, "Init Watchdog");
Watchdog.getInstance().init(context, battery, power, alarm,
ActivityManagerService.self());
Watchdog.getInstance().addThread(wmHandler, "WindowManager thread");
public static final String POWER_SERVICE = "power";
public static final String AUDIOTEST_SERVICE = "audiotest";
public static final String WINDOW_SERVICE = "window";
import android.service.audiotest.IAudioTest;
//...
registerService(POWER_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
//获得了aidl的IBinder后交给了另一个类进行封闭,保护一些敏感接口
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}});
registerService(AUDIOTEST_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(AUDIOTEST_SERVICE);
//直接把从ServiceManager获取了原始服务,并把这个服务无保留返回至用户层
IAudioTest service = IAudioTest.Stub.asInterface(b);
return service;
}});
//...
#define AUDIO_TEST_NAME "/dev/audiotest"
namespace android
{
//driver file description
static int fd = -1;
static jint AudioTestInit(JNIEnv *env, jobject thiz)
{
fd = open(AUDIO_TEST_NAME, O_RDWR);
if(fd < 0)
{
ALOGE("Open AUDIO_TEST_NAME failed!");
return -1;
}
return fd;
}
static jint AudioTestCtrl(JNIEnv *env, jobject thiz, int cmd, long arg)
{
ALOGI("AudioTestCtrl %d", cmd);
return ioctl(fd, cmd, (unsigned long)arg);
}
static jint AudioTestUninit(JNIEnv *env, jobject thiz)
{
close(fd);
return 0;
}
static JNINativeMethod method_table[] = {
//此Native函数与Java调用函数名对应
{ "AudioTestInitNative", "()I", (void*)AudioTestInit },
{ "AudioTestUninitNative", "()I", (void*)AudioTestUninit },
{ "AudioTestCtrlNative", "(IJ)I", (void*)AudioTestCtrl },
};
int register_android_server_audiotest_AudioTestService(JNIEnv *env)
{
//method_table与Java包名类名绑定
return jniRegisterNativeMethods(env, "com/android/server/audiotest/CarAudioService",
method_table, NELEM(method_table));
}
};
namespace android {
/...
int register_android_service_eposervice(JNIEnv* env);
int register_android_server_audiotest_AudioTestService(JNIEnv* env);
};
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
//...
register_android_server_ConsumerIrService(env);
register_android_service_eposervice(env);
register_android_server_audiotest_AudioTestService(env);
/...
return JNI_VERSION_1_4;
}
LOCAL_SRC_FILES:= \
com_android_server_power_PowerManagerService.cpp \
com_android_server_caraudio_CarAudioService.cpp \
com_android_server_SerialService.cpp \
//...
package android.service.audiotest;
interface IAudioTest {
int init();
// int chooseMainSrc(long src);
int mainChooseChannel(long srcId);
int mainInputGain(long gain);
//...
}
LOCAL_SRC_FILES += \
core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \
core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl \
core/java/android/accounts/IAccountManager.aidl \
core/java/android/service/audiotest/IAudioTest.aidl \
core/java/android/view/accessibility/IAccessibilityInteractionConnection.aidl\
package com.android.server.audiotest;
import android.service.audiotest.IAudioTest;
import android.content.Context;
public class AudioTestService extends IAudioTest.Stub{
private String TAG = "AudioTestService ";
private Context context;
private native int AudioTestInitNative();
private native int AudioTestUninitNative();
private native int AudioTestCtrlNative(int cmd, long arg);
public AudioTestService(Context c){
context = c;
init();
}
private int myAudioTestCtrl(AudioCtrl ac, long arg){
return AudioTestCtrlNative(ac.ordinal(), arg);
}
@Override
public int init(){
int ret = 0;
AudioTestInitNative();
return ret;
}
@Override
public int mainChooseChannel(long srcId){
int ret;
ret=myAudioTestCtrl(AudioCtrl.IOCTL_MAIN_CHANNEL, srcId);
return ret;
}
@Override
public int mainInputGain(long gain){
int ret;
ret= myAudioTestCtrl(AudioCtrl.IOCTL_MAIN_INPUT_GAIN, gain);
return ret;
}
}
//....
AlarmManagerService alarm = null;
AudioTestService audiotest = null;
MountService mountService = null;
//....
Slog.i(TAG, "Alarm Manager");
alarm = new AlarmManagerService(context);
ServiceManager.addService(Context.ALARM_SERVICE, alarm);
audiotest = new AudioTestService(context);
ServiceManager.addService(Context.AUDIOTEST_SERVICE, audiotest);
Slog.i(TAG, "Init Watchdog");
Watchdog.getInstance().init(context, battery, power, alarm,
ActivityManagerService.self());
Watchdog.getInstance().addThread(wmHandler, "WindowManager thread");
public static final String POWER_SERVICE = "power";
public static final String AUDIOTEST_SERVICE = "audiotest";
public static final String WINDOW_SERVICE = "window";
import android.service.audiotest.IAudioTest;
//...
registerService(POWER_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
//获得了aidl的IBinder后交给了另一个类进行封闭,保护一些敏感接口
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}});
registerService(AUDIOTEST_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(AUDIOTEST_SERVICE);
//直接把从ServiceManager获取了原始服务,并把这个服务无保留返回至用户层
IAudioTest service = IAudioTest.Stub.asInterface(b);
return service;
}});
//...
#define AUDIO_TEST_NAME "/dev/audiotest"
namespace android
{
//driver file description
static int fd = -1;
static jint AudioTestInit(JNIEnv *env, jobject thiz)
{
fd = open(AUDIO_TEST_NAME, O_RDWR);
if(fd < 0)
{
ALOGE("Open AUDIO_TEST_NAME failed!");
return -1;
}
return fd;
}
static jint AudioTestCtrl(JNIEnv *env, jobject thiz, int cmd, long arg)
{
ALOGI("AudioTestCtrl %d", cmd);
return ioctl(fd, cmd, (unsigned long)arg);
}
static jint AudioTestUninit(JNIEnv *env, jobject thiz)
{
close(fd);
return 0;
}
static JNINativeMethod method_table[] = {
//此Native函数与Java调用函数名对应
{ "AudioTestInitNative", "()I", (void*)AudioTestInit },
{ "AudioTestUninitNative", "()I", (void*)AudioTestUninit },
{ "AudioTestCtrlNative", "(IJ)I", (void*)AudioTestCtrl },
};
int register_android_server_audiotest_AudioTestService(JNIEnv *env)
{
//method_table与Java包名类名绑定
return jniRegisterNativeMethods(env, "com/android/server/audiotest/CarAudioService",
method_table, NELEM(method_table));
}
};
namespace android {
/...
int register_android_service_eposervice(JNIEnv* env);
int register_android_server_audiotest_AudioTestService(JNIEnv* env);
};
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
//...
register_android_server_ConsumerIrService(env);
register_android_service_eposervice(env);
register_android_server_audiotest_AudioTestService(env);
/...
return JNI_VERSION_1_4;
}
LOCAL_SRC_FILES:= \
com_android_server_power_PowerManagerService.cpp \
com_android_server_caraudio_CarAudioService.cpp \
com_android_server_SerialService.cpp \
//...