+
frameworks/base/core/java/android/app/HelloWorldManager.java
package android.app;
import android.os.RemoteException;
import android.annotation.SystemService;
import android.content.Context;
@SystemService(Context.HELLO_SERVICE)
public final class HelloWorldManager{
private final IHelloWorldManager mService;
private Context mContext;
/**
* @hide to prevent subclassing from outside of the framework
*/
HelloWorldManager(Context context,IHelloWorldManager service){
mContext = context;
mService = service;
}
/**
* Like {@link #HelloWorldManager( )}, but allowing the caller to specify
* that the vibration is owned by someone else.
* @hide
*/
public void printHello(){
try{
mService.printHello();
}catch (RemoteException ex){
}
}
}
frameworks/base/core/java/android/app/IHelloWorldManager.aidl
package android.app;
/**
* System private API for talking with the helloworld service.
* {@hide}
*/
interface IHelloWorldManager{
void printHello();
}
frameworks/base/services/core/java/com/android/server/HelloWorldService.java
package com.android.server;
import android.app.IHelloWorldManager;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
import android.content.Context;
import com.android.server.SystemService;
public class HelloWorldService extends IHelloWorldManager.Stub {
private final static String LOG_TAG = "HelloWorldService";
private static final int BACKGROUND_USER_ID = -10;
private final Object mLock = new Object();
private final Context mContext;
HelloWorldService(Context context) {
mContext = context;
}
@Override
public void printHello() throws RemoteException {
Slog.i(LOG_TAG,"xuyong Hello,World!");
}
}
当然不要忘了frameworks/base/Android.mk 中添加aidl core/java/android/app/IHelloWorldManager.aidl \
如果上述操作做完编译后无误的话至少hello service现在是在系统中的
在添加te文件时有两种思路 一种是按照system/sepolicy 目录中添加vibrator service的步骤添加hello service
另一种是在device 目录下添加hello.te 文件等操作实现。下面先说简单的:在system/sepolicy 目录中添加
system/sepolicy/prebuilts/api/26.0/nonplat_sepolicy.cil
system/sepolicy/prebuilts/api/26.0/private/service_contexts
system/sepolicy/prebuilts/api/26.0/public/service.te
system/sepolicy/private/compat/26.0/26.0.cil
system/sepolicy/private/service_contexts
system/sepolicy/public/service.te
此步骤很简单 就完整照Vibrator 添加 注意一点 :hello" 对应 Context 中的HELLO_SERVICE
system/sepolicy/private/service_contexts
hello u:object_r:hello_service:s0
system/sepolicy/prebuilts/api/26.0/public/service.te
type hello_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
文件修改后可以mmm system/sepolicy/ 验证语法或规则是否符合要求。
下面介绍另一种添加te文件方式 我们是以sprd的代码为例
device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts
device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te
device/sprd/sharkl2/common/plat_sepolicy/public/service.te
device/sprd/sharkl2/common/sepolicy/system_server.te
device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts
hello u:object_r:hello_service:s0
device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te添加
allow system_server hello_service:service_manager { add };
device/sprd/sharkl2/common/plat_sepolicy/public/service.te添加
type hello_service, service_manager_type;
device/sprd/sharkl2/common/sepolicy/system_server.te添加
allow system_server hello_service:service_manager add;
添加两个文件
device/sprd/sharkl2/common/plat_sepolicy/private/hello.te
typeattribute hello coredomain;
init_daemon_domain(hello)
device/sprd/sharkl2/common/plat_sepolicy/public/hello.te
type hello, domain;
type hello_exec, exec_type, file_type;
文件修改后可以mmm system/sepolicy/ 验证语法或规则是否符合要求.
2.完整编译 刷机 开机后 使用adb shell 进入后 手机 service list | grep hello 如果能显示说明系统中已添加成功hello service
3.编写测试程序 使用hello service 我是在Settings 中添加的
private void callHelloService(){
Log.d("xuyong","onclick begin");
HelloWorldManager hello = ((HelloWorldManager)mContext.getSystemService(Context.HELLO_SERVICE));
if (hello != null){
Log.d("xuyong","callHelloService successful!");
hello.printHello();
}
Log.d("xuyong","onclick end");
}
记得import android.app.HelloWorldManager;
最后上传源码:
下载源码
https://download.csdn.net/download/yong_xu/10519366