[Framework] 添加系统服务

栏目: Android · 发布时间: 6年前

内容简介:做系统开发,有时候需要自己定义一些接口供App使用, 同时为了方便维护管理,就会需要自己建立一个服务,把新的功能集中在一起。下面就是新建一个系统服务的基本步骤。

做系统开发,有时候需要自己定义一些接口供App使用, 同时为了方便维护管理,就会需要自己建立一个服务,把新的功能集中在一起。下面就是新建一个系统服务的基本步骤。

  1. 添加接口

    frameworks/base/core/java/android/app/IDemoManager.aidl

    package android.app;
    
    interface IDemoManager
    {
        int getCpuTemperature();
    }
    
  2. 添加服务,实现aidl文件定义的接口

    frameworks/base/services/core/java/com/android/server/DemoManagerService.java

    package com.android.server;
    
    import android.app.IDemoManager;
    import android.content.Context;
    import android.util.Slog;
    
    public class DemoManagerService extends IDemoManager.Stub {
        private Context mContext;
    
        public DemoManagerService(Context context) {
            mContext = context;
            Slog.d("Demo", "Construct");
        }
    
        @Override
        public int getCpuTemperature() {
            return 100; // Test code
        }
    }
    
  3. 添加对应的Manager

    frameworks/base/core/java/android/app/DemoManager.java

    package android.app;
    
    import android.content.Context;
    import android.os.RemoteException;
    import android.util.Slog;
    
    public class DemoManager {
        Context mContext;
        IDemoManager mService;
    
        public DemoManager(Context context, IDemoManager service) {
            mContext = context;
            mService = service;
        }
    
        public int getCpuTemperature() {
            if (mService != null) {
                try {
                    return mService.getCpuTemperature();
                } catch (RemoteException e) {
                    Slog.e("Demo", "RemoteException " + e);
                }
            }
            return -1;
        }
    }
    
  4. 添加aidl到Makefile src

    frameworks/base/Android.mk

    LOCAL_SRC_FILES += \
        core/java/android/app/IDemoManager.aidl \
    
  5. 添加DEMO_SERVICE常量

    frameworks/base/core/java/android/content/Context.java

    public static final String DEMO_SERVICE = "demo";
    
  6. 注册系统服务

    frameworks/base/core/java/android/app/SystemServiceRegistry.java

    registerService(Context.ORISLINK_SERVICE, DemoManager.class,
        new CachedServiceFetcher<DemoManager>() {
            @Override
            public DemoManager createService(ContextImpl ctx) {
                IBinder b = ServiceManager.getService(Context.DEMO_SERVICE);
                return new DemoManager(ctx, IDemoManager.Stub.asInterface(b));
    }});
    
  7. 开机启动服务

    frameworks/base/services/java/com/android/server/SystemServer.java

    try {
        ServiceManager.addService(Context.DEMO_SERVICE, new DemoManagerService(context));
    } catch (Throwable e) {
        Slog.e("Demo", "Failed to start Demo Service " + e);
    }
    
  8. 编译源码,因为添加了接口,所以需要

    make update-api
    

    更新接口。然后再整编刷机。

  9. service list 查看服务,不存在,这是因为selinux权限没加。

  10. 添加sepolicy权限

    device/qcom/sepolicy/msm8937/service.te

    type demo_service, system_api_service, system_server_service, service_manager_type;
    

    device/qcom/sepolicy/msm8937/service_contexts

    demo  u:object_r:demo_service:s0
    
  11. 重新编译代码,使用下面测试代码验证

    import android.app.DemoManager;
    
    DemoManager om = (DemoManager) getSystemService(Context.DEMO_SERVICE);
    Log.d(TAG, "Current temperature is " + om.getCpuTemperature());
    

    最终log打印出100,服务添加完成。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Book of CSS3

The Book of CSS3

Peter Gasston / No Starch Press / 2011-5-13 / USD 34.95

CSS3 is the technology behind most of the eye-catching visuals on the Web today, but the official documentation can be dry and hard to follow. Luckily, The Book of CSS3 distills the heady technical la......一起来看看 《The Book of CSS3》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具