ServiceManager反射

Android2023年8月21日 am8:08发布11个月前更新 3XCN.COM站长
0 0 0
广告也精彩
目录

前言

反射是很常用的一种方式。

今天就记录一下,反射ServiceManager的addService()和getService()。

如果不反射,需要引入

framework_classes.jar

正常情况下,如果没有系统源码的编译,不太可能拿到framework_classes.jar。

正文

我是一个lib库中需要这个ServiceManager的添加和获取服务方法,不太想导入framework_classes.jar,因此才考虑反射。

addService()

public static void addService(String name, IBinder binder) {
    try {
        Class<?> forName = Class.forName("android.os.ServiceManager");
        Method method = forName.getMethod("addService", String.class, IBinder.class);
        method.invoke(null, name, binder);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

getService()

private static IBinder getService(String name) {
    try {
        Class<?> forName = Class.forName("android.os.ServiceManager");
        Method method = forName.getMethod("getService", String.class);
        return (IBinder) method.invoke(null, name);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

参考文章

  1. Java的反射简介

 历史上的今天

版权声明 1、 本站名称: 91易搜
2、 本站网址: 91es.com3xcn.com
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
广告也精彩

相关文章

广告也精彩

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...

网站升级中

公告

近期网站升级中,可能存在一些bug。欢迎反馈 https://www.91es.com/we.html

本站域名

本站域名 : 91es.com3xcn.com。本站邮箱 : 站长邮箱 i@oorr.cn,通知邮箱we@oorr.cn ,如有更新,请看公告 。