Android 7.0 添加service,
首先我列出一下自定义service需要配置的文件
1,frameworks/base/core/java/android/app/customized/ICustomizedService.aidl
2,frameworks/base/core/java/android/app/customized/CustomizedManager.java
3,frameworks/base/services/core/java/com/android/server/customized/CustomizedService.java
4,frameworks/base/Android.mk
5,frameworks/base/core/java/android/app/SystemServiceRegistry.java
6,frameworks/base/services/java/com/android/server/SystemServer.java
7,frameworks/base/preloaded-classes
8,frameworks/base/core/java/android/content/Context.java
9,system/sepolicy/service.te
10,system/sepolicy/service_contexts
11,frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
文件列出后,咱们一个一个的把文件配置完成
首先在指定路径下新建frameworks/base/core/java/android/app/customized/ICustomizedService.aidl
package android.app.customized;
interface ICustomizedService{
void shutDown();
}
然后在指定路径下创建frameworks/base/core/java/android/app/customized/CustomizedManager.java,并实现接口
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 Context mContext;
public static class Lifecycle extends SystemService {
private CustomizedService mService;
public Lifecycle(Context context) {
super(context);
}
@Override
public void onStart() {
mService = new CustomizedService (getContext());
publishBinderService(Context.CUSTOMIZED, mService);
}
@Override
public void onBootPhase(int phase) {
}
@Override
public void onUnlockUser(int userHandle) {
}
}
public CustomizedService (Context context) {
mContext = context;
}
/**
* 1 add interface shutDown()
*/
public void shutDown () {
sentControl("custom_shutdown", "null",true);
}
private void sentControl(String action, String key, boolean iscontrol) {
long jh = Binder.clearCallingIdentity();
Intent i = new Intent();
i.setAction(action);
if (iscontrol) {
i.putExtra(key, true);
} else {
i.putExtra(key, false);
}
mContext.sendBroadcast(i);
sentControl (action,iscontrol);
Binder.restoreCallingIdentity(jh);
}
private void sentControl(String action, boolean iscontrol) {
long jh = Binder.clearCallingIdentity();
int key;
if (iscontrol) {
key = 0;
} else {
key = 1;
}
Log.i ("custom",",action "+ action + "; key"+key);
Settings.Secure.putInt(mContext.getContentResolver(),action,key);
Binder.restoreCallingIdentity(jh);
}
}
修改te文件,首先修改system/sepolicy/service.te
type custom_service, app_api_service, system_server_service, service_manager_type;
修改system/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
import android.app.customized.ICustomizedManager;
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
private static final String CUSTOM_SERVICE_CLASS =
"com.android.server.customized.customizedService$Lifecycle"; //记得声明Lifecycle
traceBeginAndSlog("StartCustomizedService");
try {
mSystemServiceManager.startService(CUSTOM_SERVICE_CLASS);
} catch (Throwable e) {
reportWtf("startingCustomizedService", e);
}
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
在frameworks/base/preloaded-classes中将自定义的service stub添加进来
android.app.customized.CustomizedManager
android.app.customized.ICustomizedService$Stub
android.app.customized.ICustomizedService$Stub$Proxy
在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中接收广播
IntentFilter filter = new IntentFilter();
filter.addAction("custom_shutdown"); //将广播添加到filter
Intent intent = context.registerReceiver(mDockReceiver, filter);
然后在mDockReceiver中接收广播,并进行处理
if(intent.getAction.equals("custom_shutdown")) mWindowManagerFuncs.shutdown(false);
然后编译一下,service就可以启动了~~~