前一篇讲了android添加服务的步骤,今天看到了一个实例,简单明了,相信有所帮助!
1. 创建frameworks\base\services\jni\com_android_server_notifyservice.cpp
static JNINativeMethod method_table[] = {
/* name, signature, funcPtr */
{ "WaitForSingleObject", "(II)I", (void *)android_NotificationService_WaitForSingleObject },
{ "SetEvent", "(I)I", (void *)android_NotificationService_SetEvent },
{ "CreateEvent", "()I", (void *)android_NotificationService_CreateEvent },
{ "CloseHandle", "(I)I", (void *)android_NotificationService_CloseHandle },
};
int register_android_server_notifyservice( JNIEnv *env )
{
return jniRegisterNativeMethods(env, "com/android/server/NotificationService",
method_table, NELEM(method_table));
}
};
2. frameworks\base\services\jni\Android.mk文件添加com_android_server_notifyservice.cpp
3. 创建frameworks\base\services\java\com\android\server\NotificationService.java
package com.android.server;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.HashMap;
import android.util.Log;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Service;
import android.app.Notification;
import android.app.INotificationService;
import android.app.INotificationCallback;
import android.content.Intent;
import android.content.Context;
import android.os.RemoteCallbackList;
import java.security.GeneralSecurityException;
public class NotificationService extends INotificationService.Stub{
private Context m_Context = null;
private boolean m_exit = false;
private INotificationCallback m_cb = null;
private static final String VERSION = "1.0.0";
private static final String TAG = "NotificationService";
private static final boolean DEBUG = true;
private int m_handle;
private List<Map<String,Object>> m_List = new Vector<Map<String,Object>>();
private List<Map<String,Object>> m_Callbacklist = new Vector<Map<String,Object>>();
private final RemoteCallbackList<INotificationCallback> m_Callbacks = new RemoteCallbackList<INotificationCallback>();
native private static int CreateEvent( );
native private static int SetEvent( int handle );
native private static int CloseHandle( int handle );
native private static int WaitForSingleObject( int handle, int timeout );
4. 添加AIDL frameworks\base\core\java\android\app\INotificationService.aidl
5. 修改frameworks\base\Android.mk文件添加INotificationService.aidl
6. 修改frameworks\base\services\java\com\android\server\SystemServer.java public void run()函数
DevicePolicyManagerService devicePolicy = null;
StatusBarManagerService statusBar = null;
InputMethodManagerService imm = null;
AppWidgetService appWidget = null;
NotificationManagerService notification = null;
NotificationService m_NotificationService = null; //liguochen
try {
Slog.i(TAG, "Notification Manager");
notification = new NotificationManagerService(context, statusBar, lights);
ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
networkPolicy.bindNotificationManager(notification);
} catch (Throwable e) {
reportWtf("starting Notification Manager", e);
}
//liguochen
try {
m_NotificationService = new NotificationService( context );
ServiceManager.addService( "JBS-NotificationService", m_NotificationService);
} catch (Throwable e) {
reportWtf("starting NotificationService Manager", e);
}
try {
Slog.i(TAG, "Device Storage Monitor");
ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
new DeviceStorageMonitorService(context));
} catch (Throwable e) {
reportWtf("starting DeviceStorageMonitor service", e);
}