新增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);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值