本文的源代码地址是:http://download.youkuaiyun.com/detail/yongyu_it/9544336
基于《HAL开发全流程(二)》,为了让Android服务更好用。我们可以封装Service实例获取的过程,交给Context管理。
注意,这一步并非必需,按照《HAL开发全流程(二)》里面开发好的Service托管给SystemServer就可以调用了。本篇日志这样做只是更加方便上层使用系统服而已。
1、开发服务管理类 JoffeeManager.java
/*
* Copyright (C) 2008 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.os;
import android.os.IJoffeeService;
import android.util.AndroidException;
import android.util.Log;
import android.util.Slog;
import android.content.Context;
import android.os.RemoteException;
/**
* The Manager class;
* The public methods of this class will be part of the new system API
*
* Have a look at how decorators are used (eg. hide) to see how mwthods are
* exported or not in the SDK;
*/
public class JoffeeManager {
private final Context mContext;
private final IJoffeeService mService;
/**
* Initialize the remote service and execution context
* ContextImpl will build this manager object and provide the
* remote service stub as parameter
*
* @param ctx
* @param service
*/
public JoffeeManager(Context ctx, IJoffeeService service) {
mContext = ctx;
mService = service;
}
/**
* Perform a call to the remote service;
* We have only one method, call it!
*/
public void callJoffeeMethod() {
try{
mService.callJoffeeMethod();
} catch (RemoteException ex){
Slog.e("JoffeManager", "Unable to contact the remote Joffee Service");
}
}
/**
* This method (which does nothing)
* is hidden from system API
* Have a look at 'reflection' to see how you can
* access hidden class members
*
* @hide
*/
public void hiddenMethod(){
Slog.d("JoffeeManager", "Hello I'm hidden");
}
}
2、参照 3.1.1 将这个文件放在 根/frameworks/base/core/java/android/os下面
不同于 3.1.2, 根/frameworks/base/core/java下的java文件已被批量添加,所以不用单独将这个java文件添加到编译文件
3、改写 根/frameworks/base/core/java/android/app/SystemServiceRegistry.java 在static语句块里面实例化系统服务备用
registerService(Context.JOFFEE_SERVICE, JoffeeManager.class,
new CachedServiceFetcher<JoffeeManager>() {
@Override
public JoffeeManager createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(Context.JOFFEE_SERVICE);
return new JoffeeManager(ctx, IJoffeeService.Stub.asInterface(b));
}});
4、参照2.3.3和2.3.4编译即可