Android 6.0系统添加自定义service

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了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值