• Android驱动学习-APP操作新硬件的两种方法(支持添加的驱动)


    在给Android添加新的驱动后,app要如何使用呢?

    正常的使用一个设备,需要getService。但是像LED等我们自己添加的硬件驱动,Android源代码根本没有我们自己添加的服务。

    第一种:

    我们自己的创建的硬件设备驱动的类是被系统定义为了隐藏类,那么在Android系统中如何使用隐藏类呢?为此我们可以根据android的编译过程可以看到我们添加的类被添加到framework.jar。但是framework.jar是dex格式,在我们做app时要使用原生态的jar目标文件.

    所以我们使用 out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/class.jar这个jar文件。

    如何包含jar文件,我们可以Google。

    第一步,把jar包放在工程下的libs目录里,如果没有就新建这个目录。

    第二步,通过File菜单或者F4进入project structure,先在左上方选择+号,然后添加class.jar,然后左边选中app,再进到dependencies,点 + 选择file dependency ,再从libs目录选择你要的jar包,选择后把 scope置为Provide。即可。
    第二种:
    通过反射来创建我们新建的类。也就是不用导入class.jar文件就能实现对我们自定义的类的创建和函数调用。
    我个人的理解就是使用 Class.forName静态函数,通过已知的函数名来实现对函数的调用。
     

    Class.forName:返回与给定的字符串名称相关联接口的Class对象。

    Class.forName是一个静态方法,同样可以用来加载类。该方法有两种形式:Class.forName(String name, boolean initialize, ClassLoader loader)和 Class.forName(String className)。第一种形式的参数 name表示的是类的全名;initialize表示是否初始化类;loader表示加载时使用的类加载器。第二种形式则相当于设置了参数 initialize的值为 true,loader的值为当前类的类加载器。

    static Class<?>

    forName(String className)

    Returns the Class object associated with the class or interface with the given string name.

    static Class<?>

    forName(String name, boolean initialize, ClassLoader loader)

    Returns the Class object associated with the class or interface with the given string name, using the given class loader.

    通过类名获取类。

    Class serviceManager = Class.forName("android.os.ServiceManager");

    获取方法

    Method method = serviceManager.getMethod("getService", String.class);

    调用方法

    method.invoke(serviceManager.newInstance(), "phone");

    ()http://blog.csdn.net/fengyuzhengfan/article/details/38086743

     
  • 相关阅读:
    vue-learning:8-template-v-on-and-modifier
    vue-learning:7-template-v-bind-with-class-and-style
    vue-learning:6-template-v-bind
    vue-learning:5-template-v-for
    Bootstrap 导航栏
    Bootstrap 导航元素
    Bootstrap 输入框组
    Bootstrap 按钮下拉菜单
    Bootstrap 按钮组
    Bootstrap 下拉菜单(Dropdowns)
  • 原文地址:https://www.cnblogs.com/ynxf/p/8215194.html
Copyright © 2020-2023  润新知