欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 源码添加一个自定义的系统服务

程序员文章站 2022-07-04 22:27:00
做 android 开发,经常用到 xxxManager ,如果要添加一个自定义的系统服务,该如何操作呢?基于 AN 6.0 ,新增 ThunderManager ,修改如下:1.创建 IThunderService.aidl在 frameworks/base/core/java/android/os/ 目录创建 IThunderService.aidlpackage android.os;// Declare any non-default types here with import sta...

做 android 开发,经常用到 xxxManager ,如果要添加一个自定义的系统服务,该如何操作呢?

基于 AN 6.0 ,新增 ThunderManager ,修改如下:

1.创建 IThunderService.aidl

frameworks/base/core/java/android/os/ 目录创建 IThunderService.aidl

package android.os;

// Declare any non-default types here with import statements

interface IThunderService {

        /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
         
        void prepareThunder();
         
        void setThunderLevel(int level);
   
        int getThunderLevel();

        void justThunder();
}

添加了四个方法。

2.将 IThunderService.aidl 加入编译

修改 frameworks/base/Android.mk 文件,
LOCAL_SRC_FILES += \ 后参考core/java/android/os/IUserManager.aidl \ 的写法,
添加一行

core/java/android/os/IThunderService.aidl \

3.创建 ThunderManager.java

frameworks/base/core/java/android/app/ 目录创建 ThunderManager.java

package android.app;

import android.content.Context;
import android.os.IThunderService;
import android.util.Log;

public class ThunderManager{

        private IThunderService tService;

        public ThunderManager(Context context, IThunderService iThunderService){
                tService = iThunderService;
        }

        public void prepareThunder(){
			try{
            		tService.prepareThunder();
        		}catch(Exception ep){
            		Log.e("ThunderManager ep:", ep.toString());
            		ep.printStackTrace();
        		}
        }
         
        public void setThunderLevel(int level){
                try{
            tService.setThunderLevel(level);
       	 }catch(Exception eset){
            Log.e("ThunderManager eset:", eset.toString());
            eset.printStackTrace();
        }
        }
   
        public int getThunderLevel(){
                try{
            return tService.getThunderLevel();
        }catch(Exception eget){
            Log.e("ThunderManager eget:", eget.toString());
            eget.printStackTrace();
        }
                return 0;
        }

        public void justThunder(){
                try{
            tService.justThunder();
        }catch(Exception ejust){
            Log.e("ThunderManager ejust:", ejust.toString());
            ejust.printStackTrace();
        }
	}
}

4.创建 ThunderService

frameworks/base/services/core/java/com/android/server/ 目录创建 ThunderService.java

package com.android.server;

import android.content.Context;
import android.os.RemoteException;
import android.os.IThunderService;

public class ThunderService extends IThunderService.Stub {

        private Context tContext;

        public ThunderService(Context context){
                tContext = context;
        }

        @Override
        public void prepareThunder() throws RemoteException {

        }

        @Override
        public void setThunderLevel(int level) throws RemoteException {

    }

        @Override
        public int getThunderLevel() throws RemoteException {
        return (int)76;
    }

        @Override
        public void justThunder() throws RemoteException {

        }

}

构造函数是否传入 Context 参数可按照需求来定,参考系统其他 Service 的写法,是会传入的,本例也传入;
为方便添加,方法体暂时为空。

5.将自定义 ThunderService 加入到 SystemServer

5.1 修改 frameworks/base/core/java/android/content/Context.java ,添加如下:

+       
+       /**
+     * Add for ThunderService
+     */
+       public static final String THUNDER_SERVICE = "thunder";

5.2 修改 frameworks/base/services/java/com/android/server/SystemServer.java ,在 startOtherServices() 方法中添加如下

try {
                Slog.i(TAG, "Thunder Service");
                ServiceManager.addService(Context.THUNDER_SERVICE,
                    new ThunderService(context));
            } catch (Throwable e) {
                reportWtf("starting Thunder Service", e);
            }

可参考原生的 LocationManagerService 添加,格式类似。

6.创建 ThunderManager.java

frameworks/base/core/java/android/app/ 目录创建 ThunderManager.java ,内容如下:

package android.app;

import android.content.Context;
import android.os.IThunderService;
import android.util.Log;

public class ThunderManager{
	
	private IThunderService tService;
	
	public ThunderManager(Context context, IThunderService iThunderService){
		tService = iThunderService;
	}
	
	public void prepareThunder(){
		try{
            tService.prepareThunder();
        }catch(Exception ep){
            Log.e("ThunderManager ep:", ep.toString());
            ep.printStackTrace();
        }
	}
	 
	public void setThunderLevel(int level){
		try{
            tService.setThunderLevel(level);
        }catch(Exception eset){
            Log.e("ThunderManager eset:", eset.toString());
            eset.printStackTrace();
        }
	}
   
	public int getThunderLevel(){
		try{
            return tService.getThunderLevel();
        }catch(Exception eget){
            Log.e("ThunderManager eget:", eget.toString());
            eget.printStackTrace();
        }
		return 0;
	}
	
	public void justThunder(){
		try{
            tService.justThunder();
        }catch(Exception ejust){
            Log.e("ThunderManager ejust:", ejust.toString());
            ejust.printStackTrace();
        }
	}
}

7.将 ThunderManager 注册到 SystemService

修改 frameworks/base/core/java/android/app/SystemServiceRegistry.java 文件,添加

import android.os.IThunderService;

registerService(Context.THUNDER_SERVICE, ThunderManager.class,
			new CachedServiceFetcher<ThunderManager>() {
			@Override
			public ThunderManager createService(ContextImpl ctx) {
				IBinder iBinder = ServiceManager.getService(Context.THUNDER_SERVICE);
                IThunderService service = IThunderService.Stub.asInterface(iBinder);
				return new ThunderManager(ctx, service);
			}});

8.添加 selinux 权限

8.1 修改 external/sepolicy/service.te ,最后面添加一行

type thunder_service, system_api_service, system_server_service, service_manager_type;

8.2 修改 external/sepolicy/service_contexts ,参考 search 添加一行

thunder                                   u:object_r:thunder_service:s0

9.重新编译系统api : make update-api

修改 framework 下的源码后,需要先执行 make update-api ,
此操作会如下文件:

frameworks/base/api/system-current.txt
frameworks/base/api/system-removed.txt

然后再执行 make 编译即可。

10.编写 apk 测试

在源码的 out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/ 目录下会生成 classes.jar 文件,
这个就是编译生成的 framework 依赖包,将其导入 AndroidStudio 工程就可以快乐的使用 ThunderManager 了。
代码示例:

ThunderManager manager = (ThunderManager)getSystemService("thunder");
//ThunderManager manager = (ThunderManager)getSystemService(Context.THUNDER_SERVICE);
 int level = manager.getThunderLevel();
Log.d("luoah", "[MainActivity.java] -- test -- 1 level:" + level);
manager.setThunderLevel(98);
manager.prepareThunder();
 manager.justThunder();

初始化的标准写法是:

ThunderManager manager = (ThunderManager)getSystemService(Context.THUNDER_SERVICE);

AndroidStudio 里未导入 Context 对应的 jar 包,所以直接用,

ThunderManager manager = (ThunderManager)getSystemService("thunder");

如果是在源码编译环境下,不用导包,直接用标准写和编译即可。

这个是添加了系统服务,但没有交互,如何交互,先容我学习学习。

参考:

  1. https://www.cnblogs.com/liam999/p/5933827.html
  2. https://www.cnblogs.com/chorm590/p/7258001.html

本文地址:https://blog.csdn.net/weixin_44021334/article/details/107180719