[android]Framework新增系统服务

在android源码中增加一项系统服务,如在android.app包下增加DevInfoManager.java,按如下进行处理:

1、在frameworks/base/core/java/android/app/目录下增加DevInfoManager.java及IDevInfoManager.aidl文件

DevInfoManager.java如下:

package android.app;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
public class DevInfoManager
{
        public static final String TAG = "DevInfoManager";
        IDevInfoManager mService;
        Handler mHandler;

        public DevInfoManager(IDevInfoManager service, Handler handler) {
                mService = service;
                mHandler = handler;
        }

        public String getValue(String name){
            try {
                return mService.getValue(name);
            } catch (RemoteException e) {
                Log.e(TAG, "[getValue] RemoteException");
            }
            return null;
        }

        public int update(String name, String value, int attribute) {
            try {
                return mService.update(name, value, attribute);
            } catch (RemoteException e) {
                Log.e(TAG, "[update] RemoteException");
            }
            return -1;
        }
}

IDevInfoManager.aidl文件如下:

package android.app;

interface IDevInfoManager
{
    String getValue(String name);
    int update(String name, String value, int attribute);
}

2、添加相应的Service类,在frameworks/base/services/java/com/android/server下增加DevInfoService.java文件

DevInfoService.java文件如下:

package com.android.server;

import android.app.IDevInfoManager;
import android.util.Log;
import android.content.Context;
public class DevInfoService extends IDevInfoManager.Stub
{
    private static final String TAG = "DevInfoManagerService";
    private Context mContext;
    public DevInfoService(Context context){
        mContext = context;
    }

    public String getValue(String name) {
        Log.d(TAG,"[getValue] name : "+name);
        return name;
    }

    public int update(String name, String value, int attribute) {
        Log.d(TAG,"[update] name : "+name +", value : "+value+", attribute : "+attribute);
        return 0;
    }
}

3、在SystemServer中生成该Service并添加到ServiceManager

frameworks/base/services/java/com/android/server/SystemServer.java文件:

// add DevInfoService
try {
    Slog.i(TAG, "DevInfoManager Service");
    devInfo =  new DevInfoService(context);
    ServiceManager.addService(Context.DEVINFO_SERVICE, devInfo);
} catch (Throwable e) {
    reportWtf("starting devInfo Service", e);
}
// end

4、在frameworks/base/core/java/android/app/ContextImpl.java中增加service注册,如下:

registerService(DEVINFO_SERVICE, new ServiceFetcher() {
        public Object createService(ContextImpl ctx) {
                IBinder b = ServiceManager.getService(DEVINFO_SERVICE);
                IDevInfoManager service = IDevInfoManager.Stub.asInterface(b);
                return new DevInfoManager(service, ctx.mMainThread.getHandler());
        }
});

5、在frameworks/base/core/java/android/content/Context.java中增加Service名称定义:

/**
 * Use with {@link #getSystemService} to retrieve a {@link
 * android.app.DevInfoManager} for handling management of
 * device information access.
 *
 * @see #getSystemService
 * @see android.app.DevInfoManager
 */
public static final String DEVINFO_SERVICE = "devinfo";

6、在frameworks/base/Android.mk文件中的LOCAL_SRC_FILES增加aidl的声明:

@@ -112,6 +112,7 @@ LOCAL_SRC_FILES += \

        core/java/android/app/backup/IFullBackupRestoreObserver.aidl \

        core/java/android/app/backup/IRestoreObserver.aidl \

        core/java/android/app/backup/IRestoreSession.aidl \

+        core/java/android/app/IDevInfoManager.aidl \

        core/java/android/bluetooth/IBluetooth.aidl \

        core/java/android/bluetooth/IBluetoothA2dp.aidl \


7、进行编译,因为framework有变更,需要先update-api,在源码根目录下执行“make update-api”进行更新,完成后“make -j16”进行整体编译



你可能感兴趣的:(android)