framework添加服务(实例)

前一篇讲了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);
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值