新增Android系统服务

由于项目需求,需要在系统中新加一项系统服务DevInfoManager,特作如下记录:

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

DevInfoManager.java

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.RemoteException;
import android.util.Log;
import java.util.Properties;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * {@hide}
 */
public abstract interface DevInfoManager {

	/** @hide*/
    public static final String DATA_SERVER = "data";  
    
    /** @hide*/
	public static final int Default_Attribute = 0;
      
	/** @hide*/
	public String getValue(String name);
	
	/** @hide*/
	public int update(String name, String value, int attribute) ;
	
}

DevInfoManagerImpl.java

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.RemoteException;
import android.os.Binder;
import android.util.Log;
import java.util.Properties;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * {@hide}
 */
 public class DevInfoManagerImpl implements DevInfoManager{

	/** @hide*/
    public static final String DATA_SERVER = "data";  
    /** @hide*/
	public static final int Default_Attribute = 0;
	/** @hide*/
	private static final String LOG_TAG = "DevInfoManager";

	
    /**
     * Version number must be incremented when table structure is changed.
     * @hide
     */
    private static final int DEVINFO_MANAGER_VERSION = 1; 

	/** @hide*/
	private final Context mContext;
	private IDevInfoManager mService;
	
    @Override
    /** @hide*/
	protected void finalize() throws Throwable {
		super.finalize();
	}

    /** @hide*/
	public DevInfoManagerImp(Context context,IDevInfoManager service) {
        mContext = context;
        mService = service;
	}
	    
	/** @hide*/
	public String getValue(String name){
		String strVal = "";
		try {
			strVal = mService.getValue(name);
			if(null == strVal) {
				strVal = "";
			}
		} catch (RemoteException e) {
			Log.e(LOG_TAG, "[getValue] RemoteException "+e.getMessage());
			strVal = "";
		}
		Log.d(LOG_TAG, String.format("getValue name:%s value:%s", name, strVal));
		return strVal;
	}
	
	/** @hide*/
	public int update(String name, String value, int attribute) {
		Log.d(LOG_TAG, String.format("update name:%s value:%s attribute:%d",name, value, 
            attribute));
		int iRet = 0;
		try {
		    iRet = mService.update(name,value,attribute);
		} catch (RemoteException e) {
			Log.e(LOG_TAG, "[update] RemoteException "+e.getMessage());
			iRet = -1;
		}
		Log.d(LOG_TAG, String.format("update name:%s iRet:%d", name, iRet));
		return iRet;
	}
}

IDevInfoManager.aidl

package android.app;

/**
 * {@hide}
 */
interface IDevInfoManager {
    String getValue(String name);
    int update(String name, String value, int attribute);
}

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

package com.android.server;

import android.util.Log;
import android.app.IDevInfoManager;
import android.content.Context;

public class DevInfoManagerService extends IDevInfoManager.Stub {
    private static final String LOG_TAG = "DevInfoManagerService";
    private final Context mContext;

    public DevInfoManagerService(Context Context){
        mContext = Context;
    }
 
    public String getValue(String name) {
        return null;
    }

    public int update(String name,String value, int attribute) {                                                                                                                                     
        return 0;
    }
}

3、在frameworks/base/services/java/com/android/server/SystemServer.java文件中生成该Service并添加到ServiceManager中

Slog.i(TAG, " DevInfoManager Service");
dmService = new DevInfoManagerService(context);
ServiceManager.addService("data", dmService);
Slog.i(TAG, "System add DevInfoManager Services");

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

registerService("data", new ServiceFetcher() {
    public Object createService(ContextImpl ctx) {
        IBinder b = ServiceManager.getService("data");
        IDevInfoManager service = IDevInfoManager.Stub.asInterface(b);
        return new DevInfoManagerImp(ctx, service);
    }});

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

LOCAL_SRC_FILES += \
    core/java/android/content/EventLogTags.logtags \
    core/java/android/speech/tts/EventLogTags.logtags \
    core/java/android/webkit/EventLogTags.logtags \
    core/java/android/app/IDevInfoManager.aidl \

6、编译之前先执行make update-api

7、使用

import android.app.DevInfoManager;

DevInfoManager devInfoManager = (DevInfoManager)         
    context.getSystemService(DevInfoManager.DATA_SERVICE);
devInfoManager.getValue(key);
devInfoManager.update(key, value, attr);

 

你可能感兴趣的:(新增Android系统服务)