Android 添加自定义service,首先在指定路径下新建frameworks/base/core/java/android/app/customized/ICustomizedService.aidl和frameworks/base/core/java/android/app/customized/CustomizedManager.java文件
package android.app.customized;
interface ICustomizedService{
void shutDown();
}
package android.app.customized;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.RemoteException;
import android.provider.Settings;
import java.io.IOException;
import android.os.ServiceManager;
import android.os.IBinder;
import java.util.List;
import android.app.ActivityManager;
import android.graphics.Bitmap;
public class CustomizedManager{
private static final String TAG="CustomizedManager";
private static final boolean DBG=true;
private static ICustomizedService mService;
private final Context mContext;
public CustomizedManager(Context context){
mContext = context;
mService = ICustomizedService.Stub.asInterface(
ServiceManager.getService("customized"));
}
private static ICustomizedService getService(){
if (mService != null) {
return mService;
}
IBinder b = ServiceManager.getService("customized");
if(b==null){
System.out.println("IBinder is null");
}else{
System.out.println("IBinder is not null");
}
mService = ICustomizedService.Stub.asInterface(b);
if(mService==null){
System.out.println("mService is null");
}else{
System.out.println("mService is not null");
}
return mService;
}
public void shutDown(){
ICustomizedService service = getService();
try {
service.shutDown();
} catch (RemoteException e) {}
}
}
在如下路径创建frameworks/base/services/core/java/com/android/server/customized/CustomizedService.java
package com.android.server.customized;
import android.os.IBinder;
import android.os.ServiceManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.app.customized.ICustomizedService;
import android.content.BroadcastReceiver;
public class CustomizedService extends ICustomizedService.Stub {
private static final String TAG = "CustomizedService";
private static final boolean DBG = true;
private Context mContext;
public CustomizedService(Context context){
mContext=context;
}
public void shutDown(){
mContext.sendBroadcast(new Intent("com.customized.shutdown"));
}
}
修改te文件,我做的是MTK平台,修改的是device/mediatek/common/sepolicy/service.te
type custom_service, app_api_service, system_server_service, service_manager_type;
修改device/mediatek/common/sepolicy/service_contexts
customized_service u:object_r:custom_service:s0
在frameworks/base/Android.mk 将AIDL文件添加进去
LOCAL_SRC_FILES += \
core/java/android/app/customized/ICustomizedService.aidl \
在frameworks/base/core/java/android/app/SystemServiceRegistry.java中注册service
import android.app.customized.CustomizedManager; //记得import CustomizedManager
registerService(Context.CUSTOMIZED, CustomizedService.class,
new CachedServiceFetcher<CustomizedManager>() {
@Override
public CustomizedManager createService(ContextImpl ctx) {
return new CustomizedManager(ctx);
}});
在frameworks/base/services/java/com/android/server/SystemServer.java中将service添加进去
import com.android.server.customized.CustomizedService; //不要忘记import service
CustomizedService customizedService=null; //提前声明变量
customizedService=new CustomizedService (context);
ServiceManager.addService("customized", CustomizedService);
在/frameworks/base/core/java/android/content/Context.java中定义
public static final String CUSTOMIZED = "customized";
/** @hide */
@StringDef({
CUSTOMIZED, //添加到StringDef中
})
最后在frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java中接收广播
filter.addAction("com.customized.reboot"); //将广播添加到filter
if(intent.getAction.equals("com.customized.shutdown")) mWindowManagerFuncs.shutdown(false);
编译之后刷机就可以运行自己的service了~