如何添加一个Application Framework Service(without native code)?
1、本文参照AlarmManagerService实现一个简单的Application Framework Service,取名为HelloWorldService,类图如下:
2、在目录frameworks/base/core/java/android/app/下添加aidl文件:
IHelloWorld.aidl:
package android.app;
import java.lang.String;
interface IHelloWorld
{
void printHelloWorld();
void printString(String string);
}
需要注意:
(1) 该文件的package声明应为:package android.app;
3、在目录frameworks/base/services/java/com/android/server/下添加文件:
HelloWorldService.java:
package com.android.server;
import android.app.IHelloWorld;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
public class HelloWorldService extends IHelloWorld.Stub
{
private static final String TAG = "HelloWorldService";
private Context mContext = null;
public HelloWorldService(Context context)
{
mContext = context;
}
@Override
public void printHelloWorld() throws RemoteException
{
// TODO Auto-generated method stub
Log.d(TAG, "Hello World!");
}
@Override
public void printString(String string) throws RemoteException
{
// TODO Auto-generated method stub
Log.d(TAG, string);
}
}
需要注意:
(1)该文件的package声明应为:package com.android.server;
(2)该文件中添加语句:import android.app.IHelloWorld;
4、在目录frameworks/base/core/java/android/app/下添加文件:
HelloWorldManager.java:
package android.app;
import android.os.RemoteException;
public class HelloWorldManager
{
private static final String TAG = "HelloWorldManager";
private IHelloWorld mService = null;
HelloWorldManager(IHelloWorld service)
{
mService = service;
}
public void printHelloWorld()
{
try
{
mService.printHelloWorld();
}
catch(RemoteException e)
{
e.printStackTrace();
}
}
public void printString(String string)
{
try
{
mService.printString(string);
}
catch(RemoteException e)
{
e.printStackTrace();
}
}
}
需要注意:
(1)该文件的package声明应为:package android.app,需要与aidl文件中的package声明保持一致;
5、修改frameworks/base/下的Android.mk文件:
在“LOCAL_SRC_FILES += \”中,参照IAlarmManager的添加方式:
core/java/android/app/IAlarmManager.aidl \
在“LOCAL_SRC_FILES += \”的最后面添加:
core/java/android/app/IHelloWorld.aidl,
需要注意:
(1) 在core/java/android/app/IHelloWorld.aidl上面一行的最后面,还需要添加“\”;
6、修改frameworks/base/core/java/android/content/下的Context.java文件:
参照ALARM_SERVICE的添加方式:
public static final String ALARM_SERVICE = "alarm";
为Context 类添加成员变量:
public static final String HELLOWORLD_SERVICE = "helloworld";
7、修改frameworks/base/services/java/com/android/server/下的SystemServer.java文件:
参照AlarmManagerService的注册方式:
AlarmManagerService alarm = null;
Slog.i(TAG, "Alarm Manager");
alarm = new AlarmManagerService(context);
ServiceManager.addService(Context.ALARM_SERVICE, alarm);
将HelloWorldService注册到ServiceManager中:
HelloWorldService helloworld = null;
Slog.i(TAG, "HelloWorld Manager");
helloworld = new HelloWorldService(context);
ServiceManager.addService(Context.HELLOWORLD_SERVICE, helloworld);
8、修改frameworks/base/core/java/android/app/下的ContextImpl.java文件:
Android 4.0 ICS系统的注册方式:
参照ALARM_SERVICE的注册方式:
registerService(ALARM_SERVICE, new StaticServiceFetcher()
{
public Object createStaticService()
{
IBinder b = ServiceManager.getService(ALARM_SERVICE);
IAlarmManager service = IAlarmManager.Stub.asInterface(b);
return new AlarmManager(service);
}
}
);
注册HELLOWORLD_SERVICE:
registerService(HELLOWORLD_SERVICE, new StaticServiceFetcher()
{
public Object createStaticService()
{
IBinder b = ServiceManager.getService(HELLOWORLD_SERVICE);
IHelloWorld service = IHelloWorld.Stub.asInterface(b);
return new HelloWorldManager(service);
}
}
);
Android 2.3 gingerbread系统的注册方式:
参照ALARM_SERVICE的注册方式:
private static AlarmManager sAlarmManager;
private AlarmManager getAlarmManager()
{
synchronized (sSync)
{
if (sAlarmManager == null)
{
IBinder b = ServiceManager.getService(ALARM_SERVICE);
IAlarmManager service = IAlarmManager.Stub.asInterface(b);
sAlarmManager = new AlarmManager(service);
}
}
return sAlarmManager;
}
else if (ALARM_SERVICE.equals(name))
{
return getAlarmManager();
}
注册HELLOWORLD_SERVICE:
private static HelloWorldManager sHelloWorldManager;
private HelloWorldManager getHelloWorldManager()
{
synchronized (sSync)
{
if (sHelloWorldManager == null)
{
IBinder b = ServiceManager.getService(HELLOWORLD_SERVICE);
IHelloWorld service = IHelloWorld.Stub.asInterface(b);
sHelloWorldManager = new HelloWorldManager(service);
}
}
return sHelloWorldManager;
}
else if (HELLOWORLD_SERVICE.equals(name))
{
return getHelloWorldManager();
}
9、如果Service中不涉及到JNI操作,Application Framework Service HelloWorldService就添加完成,
然后就可以直接在 Application中像使用AlarmManager一样使用HelloWorldManager进行操作,
示例如下:
HelloWorldServiceTest.java
package com.android.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.app.HelloWorldManager;
import android.content.Context;
public class HelloWorldServiceTest extends Activity
{
private static final String TAG = "HelloWorldServiceTest";
private HelloWorldManager mHelloWorldManager = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHelloWorldManager = (HelloWorldManager)getSystemService(Context.HELLOWORLD_SERVICE);
Log.d(TAG, "******************************");
mHelloWorldManager.printHelloWorld();
Log.d(TAG, "******************************");
mHelloWorldManager.printString("I succeed!");
Log.d(TAG, "******************************");
}
}
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := HelloWorldServiceTest
include $(BUILD_PACKAGE)
程序执行结果如下图: