如何在App层添加System Service

本文介绍如何在Android系统中通过ServiceManager注册自定义服务,并提供了一个具体的例子,包括创建AIDL接口、实现服务类以及在ServiceManager中添加服务的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本原理就是调用ServiceManager中的接口

    /**
     * Place a new @a service called @a name into the service
     * manager.
     *
     * @param name the name of the new service
     * @param service the service object
     */
    public static void addService(String name, IBinder service) {
        try {
            getIServiceManager().addService(name, service, false);
        } catch (RemoteException e) {
            Log.e(TAG, "error in addService", e);
        }
    }

首先,需要写一个aidl,这个接口能生成符合System Service的Stub和Proxy,他们都继承于IBinder

/** @hide */
interface IMyExample {
int checkPermission(String permissionName, int uid);
}

这东西被aidl的解释语言搞成了IMyExample.java(./target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/com/xxxx/IMyExample .java)

/*
* This file is auto-generated. DO NOT MODIFY.
*/
package com.mobvoi;
/** @hide */
public interface IMyExample extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.mobvoi.IMobvoiPermissionManager
{

public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}

public static com.mobvoi.IMyExample asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.mobvoi.IMyExample ))) {
return ((com.mobvoi.IMyExample )iin);
}
return new com.mobvoi.IMyExample.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_checkPermission:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
int _arg1;
_arg1 = data.readInt();
int _result = this.checkPermission(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.mobvoi.IMyExample
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/**
* [Permission Controller Functions]
*/
@Override public int checkPermission(java.lang.String permissionName, int uid) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(permissionName);
_data.writeInt(uid);
mRemote.transact(Stub.TRANSACTION_checkPermission, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_checkPermission = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
/**
* [Permission Controller Functions]
*/
public int checkPermission(java.lang.String permissionName, int uid) throws android.os.RemoteException;
}

写一个类去实现这个SystemService

public class MyExample extends IMyExample .Stub {

    public MyExample (Context context) {
    }

    @Override
    public int checkPermission(String permissionName, int uid) throws RemoteException {

       }
}

在某个地方添加到ServiceManager里面

ServiceManager.addService(“Service Name”, MyExample );

另外,还要在SELinux中注册一下,这种机制也是为了避免App随便给系统添加服务

sepolicy/service/ service_contexts
Mmsstats u:object_r:system_app_service:s0
MyExample    u:object_r:system_app_service:s0

sepolicy/proxy/ service_contexts
TicwearProxyNativeService u:object_r:ticwear_proxy_service:s0
com.mobvoi.ticwear.home.proxy.IMyExample   u:object_r:system_server_service:s0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值