栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android10.0新增系统服务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android10.0新增系统服务

添加系统服务 showme 0x00 环境

aosp源码:android-10.0.0_r4

0x01 添加aidl

frameworks/base/core/java/android/os/IShowmeManager.aidl

package android.os;

interface IShowmeManager
{
	String getVal();
   
}
0x02 aidl 添加到 android.bp

frameworks/base/Android.bp

 
 java_defaults {
     name: "framework-defaults",
     installable: true,
 
     srcs: [
         // From build/make/core/pathmap.mk FRAMEWORK_BASE_SUBDIRS

        "core/java/android/os/storage/IStorageManager.aidl",
         "core/java/android/os/storage/IStorageEventListener.aidl",
         "core/java/android/os/storage/IStorageShutdownObserver.aidl",

         "core/java/android/os/IShowmeManager.aidl",

         "core/java/android/permission/IPermissionController.aidl",
         ":keystore_aidl",

0x03 创建service文件,也就是aidl的具体实现

frameworks/base/services/core/java/com/android/server/showme/ShowmeManagerService.java

  • 首先在frameworks/base/services/core/java/com/android/server/中创建showme文件夹
  • 再在showme文件夹中添加ShowmeManagerService.java文件
package com.android.server.showme;
 
import com.android.server.SystemService;
import android.content.Context;
import android.util.Log;
import java.util.HashMap;
import android.os.IShowmeManager;
 
 
public final class ShowmeManagerService extends IShowmeManager.Stub{
	
	private static final String TAG = "ShowmeManagerService";
	final Context mContext;
    public ShowmeManagerService(Context context) {
        mContext = context;				
 
    }	
    @Override
	public  String getVal(){
		
		try{
			Log.d(TAG, "GetFromJni  ");
			return "GetFromJni showme ";
		}catch(Exception e){
			Log.d(TAG, "nativeReadPwd Exception msg = " + e.getMessage());
			return " read nothings!!!";
		}
	} 	
 
}
0x04 将ShowmeManagerService添加到系统服务管理器中

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

导入具体实现

import com.android.server.showme.ShowmeManagerService;

在private void startOtherServices()中添加服务

ShowmeManagerService showmeManagerService = null;



// ShowmeManagerService begin
traceBeginAndSlog("StartShowmeManService");
try {  
if(showmeManagerService==null){            
   showmeManagerService = new ShowmeManagerService(context);
}
ServiceManager.addService("showme", showmeManagerService);
 } catch (Throwable e) {
         Slog.e(TAG, "Failure starting ShowmeManagerService ", e);
  }
traceEnd();
// ShowmeManagerService end
0x05 创建可由外部使用的接口

frameworks/base/core/java/android/os/ShowmeManager.java

package android.os;
 
import android.annotation.SystemService;
import android.content.Context;
import android.util.Log;
import android.os.Handler;
import android.os.SystemProperties;
import java.io.IOException;
import java.io.DataInputStream;
//@SystemService(Context.SHOWME_SERVICE)
public final class ShowmeManager {
    private static final String TAG = "ShowmeManager";
    final Context mContext;
    final IShowmeManager mService;
    final Handler mHandler;
    
    public ShowmeManager(Context context, IShowmeManager service, Handler handler) {
        mContext = context;
        mService = service;
        mHandler = handler;
    }
	
	public String getVal() {
		Log.d(TAG,"ShowmeManager   getVal ");
        try {
           return mService.getVal();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }	
}
0x06 在Context 配置接口名称

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

public static final String SHOWME_SERVICE = "showme";

@StringDef(suffix = { "_SERVICE" }, value = {
            VIBRATOR_SERVICE,
            SHOWME_SERVICE,
0x07 注册服务

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

import android.os.ShowmeManager;
import android.os.IShowmeManager;

// 在 final class SystemServiceRegistry 的静态块中添加
registerService(Context.SHOWME_SERVICE, ShowmeManager.class,
new CachedServiceFetcher() {
    @Override
    public ShowmeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
        IBinder b = ServiceManager.getServiceOrThrow(Context.SHOWME_SERVICE);
        IShowmeManager service = IShowmeManager.Stub.asInterface(b);
        return new ShowmeManager(ctx.getOuterContext(),
                service, ctx.mMainThread.getHandler());
    }});
0x08 新增service “showme” 的 selinux配置策略

在文件system/sepolicy/private/service_contexts中添加如下内容

showme                                  u:object_r:showme_service:s0

在文件system/sepolicy/prebuilts/api/29.0/private/service_contexts中添加如下内容

showme                                  u:object_r:showme_service:s0

在文件system/sepolicy/public/service.te中添加如下内容

type showme_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;

在文件system/epolicy/prebuilts/api/29.0/public/service.te中添加如下内容

type showme_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;

注意:如下两个文件中的内容必须完全一致,多一行空格都不行

system/sepolicy/public/service.te
system/epolicy/prebuilts/api/29.0/public/service.te
0x09 更新接口与并编译
source build/envsetup.sh
ulimit -S -n 2048
lunch aosp_x86_64-eng

# 更新接口
make update-api

# 编译
make SELINUX_IGNORE_NEVERALLOWS=true -j16
0x10 app 使用系统服务

目录out/target/common/obj/JAVA_LIBRARIESframework_intermediates中的class.jar导入到android studio

示例代码如下

ShowmeManager showmeManager = (ShowmeManager) getApplicationContext().getSystemService("showme");
String result = showmeManager.getVal();
Log.v("my_serviec","showme result = " + result);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878395.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号