使用使用"mmm frameworks/base/"命令编译

frameworks/base/core/java/android/os/

ILedService.aidl文件,在out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/目录下会生成ILedService.java文件,内容如下:

/*

* This file is auto-generated. DO NOT MODIFY.

* Original file: frameworks/base/./core/java/android/os/ILedService.aidl

*/

package android.os;

/** {@hide} */

public interface ILedService extends android.os.IInterface

{

/** Local-side IPC implementation stub class. */

public static abstract class Stub extends android.os.Binder implements android.os.ILedService

{

private static final java.lang.String DESCRIPTOR = "android.os.ILedService";

/** Construct the stub at attach it to the interface. */

public Stub()

{

this.attachInterface(this, DESCRIPTOR);

}

/**

* Cast an IBinder object into an android.os.ILedService interface,

* generating a proxy if needed.

*/

public static android.os.ILedService asInterface(android.os.IBinder obj)

{

if ((obj==null)) {

return null;

}

android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);

if (((iin!=null)&&(iin instanceof android.os.ILedService))) {

return ((android.os.ILedService)iin);

}

return new android.os.ILedService.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_ledCtrl:

{

data.enforceInterface(DESCRIPTOR);

int _arg0;

_arg0 = data.readInt();

int _arg1;

_arg1 = data.readInt();

int _result = this.ledCtrl(_arg0, _arg1);

reply.writeNoException();

reply.writeInt(_result);

return true;

}

}

return super.onTransact(code, data, reply, flags);

}

private static class Proxy implements android.os.ILedService

{

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;

}

@Override public int ledCtrl(int which, int status) 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.writeInt(which);

_data.writeInt(status);

mRemote.transact(Stub.TRANSACTION_ledCtrl, _data, _reply, 0);

_reply.readException();

_result = _reply.readInt();

}

finally {

_reply.recycle();

_data.recycle();

}

return _result;

}

}

static final int TRANSACTION_ledCtrl = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);

}

public int ledCtrl(int which, int status) throws android.os.RemoteException;

}在前面生成apk的文章中,通过

iLedService = ILedService.Stub.asInterface(ServiceManager.getService("led"));来获得一个ILedService对象,之后就可以通过这个对象的ledCtrl方法来操作LED。本文中,使用java的反射机制来生成apk,MainActivity.java源码如下:

package mobiletek.ledreflect;

import android.os.RemoteException;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Button;

import android.view.View;

import android.widget.CheckBox;

import android.widget.Toast;

import android.os.IBinder;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

private boolean ledon = false;

private Button button = null;

Object proxy = null;

Method ledCtrl = null;

private CheckBox checkBoxLed1 = null;

private CheckBox checkBoxLed2 = null;

private CheckBox checkBoxLed3 = null;

private CheckBox checkBoxLed4 = null;

int []ledctrl = {0x12, 0x34, 0x56, 0x78};

class MyButtonListener implements View.OnClickListener {

@Override

public void onClick(View v) {

ledon = !ledon;

if (ledon) {

button.setText("ALL OFF");

checkBoxLed1.setChecked(true);

checkBoxLed2.setChecked(true);

checkBoxLed3.setChecked(true);

checkBoxLed4.setChecked(true);

try {

for (int i = 0; i < 4; i++)

ledCtrl.invoke(proxy, ledctrl[i], 1);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

else {

button.setText("ALL ON");

checkBoxLed1.setChecked(false);

checkBoxLed2.setChecked(false);

checkBoxLed3.setChecked(false);

checkBoxLed4.setChecked(false);

try {

for (int i = 0; i < 4; i++)

ledCtrl.invoke(proxy, ledctrl[i], 0);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

}

}

public void onCheckboxClicked(View view) {

// Is the view now checked?

boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked

try {

switch(view.getId()) {

case R.id.LED1:

if (checked) {

Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[0], 1);

}

else {

Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[0], 0);

}

break;

case R.id.LED2:

if (checked) {

Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[1], 1);

}

else {

Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[1], 0);

}

break;

case R.id.LED3:

if (checked) {

Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[2], 1);

}

else {

Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[2], 0);

}

break;

case R.id.LED4:

if (checked) {

Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[3], 1);

}

else {

Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();

ledCtrl.invoke(proxy, ledctrl[3], 0);

}

break;

}

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button)findViewById(R.id.BUTTON);

try {

Method getService = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);

Object ledService = getService.invoke(null, "led");

Method asInterface = Class.forName("android.os.ILedService$Stub").getMethod("asInterface", IBinder.class);

proxy = asInterface.invoke(null, ledService);

ledCtrl = Class.forName("android.os.ILedService$Stub$Proxy").getMethod("ledCtrl", int.class, int.class);

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

checkBoxLed1 = (CheckBox) findViewById(R.id.LED1);

checkBoxLed2 = (CheckBox) findViewById(R.id.LED2);

checkBoxLed3 = (CheckBox) findViewById(R.id.LED3);

checkBoxLed4 = (CheckBox) findViewById(R.id.LED4);

button.setOnClickListener(new MyButtonListener());

}

}在onCreate方法中,首先通过下面两行代码将

ILedService.Stub.asInterface(ServiceManager.getService("led"));实参替换掉:

Method getService = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);

Object ledService = getService.invoke(null, "led");

ServiceManager.getService("led")被替换为ledService,

ServiceManager的getService方法在frameworks/base/core/java/android/os/ServiceManager.java文件中定义:

public static IBinder getService(String name) {

try {

IBinder service = sCache.get(name);

if (service != null) {

return service;

} else {

return getIServiceManager().getService(name);

}

} catch (RemoteException e) {

Log.e(TAG, "error in getService", e);

}

return null;

}通过proxy = asInterface.invoke(null, ledService);返回IBinder对象,再向上转换为Object对象。接下来通过下面两行,调用ILedService接口的内部类Stub的asInterface方法获得ILedService对象proxy,再向上转换为Object对象:

Method asInterface = Class.forName("android.os.ILedService$Stub").getMethod("asInterface", IBinder.class);

proxy = asInterface.invoke(null, ledService);最后通过ledCtrl = Class.forName("android.os.ILedService$Stub$Proxy").getMethod("ledCtrl", int.class, int.class);获得

ILedService接口的内部类Stub的内部类Proxy的ledCtrl方法,之后就可以通过ledCtrl.invoke来操作LED,由于ledCtrl方法没有用static声明,所以调用时需要传入一个实例化对象,例如:ledCtrl.invoke(proxy, ledctrl[0], 1);

java 操作 led_Java中使用反射机制操作LED相关推荐

  1. 浅说Java中的反射机制(一)

    在学习传智播客李勇老师的JDBC系列时,会出现反射的概念,由于又是第一次见,不免感到陌生.所以再次在博客园找到一篇文章,先记录如下: 引用自java中的反射机制,作者bingoideas.(()为我手 ...

  2. java代码安全检测机制_全面解析:java中的反射机制,内含代码验证解析

    什么是反射? 在运行状态中,对于任意一个类,都能够获取到这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性(包括私有的方法和属性),这种动态获取的信息以及动态调用对象的方法的功 ...

  3. Java中的反射机制详讲

    Java中的反射机制详讲 1.反射机制_介绍_Class对象获取 2.反射机制_动态操作_构造器_方法_属性 3.动态编译_DanamicCompile_反射调用main方法问题 好文推荐:排序.查找 ...

  4. 【反射机制】Java中的反射机制,使用反射机制创建对象、访问属性、方法、构造方法等

    这篇文章主要是整理了Java中的反射机制,包括:反射机制概念.反射机制访问构造方法.反射机制访问普通方法.反射机制访问属性,反射机制访问修饰符. 目录 一.反射机制概念 二.反射机制使用 (1)加载C ...

  5. formdata 接受参数中带有class 对象_浅析JAVA中的反射机制及对Servlet的优化

    今天来聊聊java中的反射机制,工作以后发现很多东西动不动就要使用反射或者动态代理,如果不能很好的理解反射,那么对于动态代理等一些重要的设计模式就会有种不够通透的感觉. 所谓的反射,就是在运行状态中, ...

  6. java反射机制是什么_java中的反射机制是什么?

    java中的反射机制是什么? 发布时间:2020-05-21 22:45:50 来源:亿速云 阅读:156 作者:鸽子 java:"一切即对象",感觉java语言本身在不断践行着这 ...

  7. java中的反射机制是什么

    给大家介绍一下java中的反射机制,java中反射机制更体现出了java的灵活性.多态.和类之间的耦合性. 1:反射是一种间接操作目标对象的机制,只要给定类的名字,就可以通过反设机制获取所有的类信息. ...

  8. 初探GO中的反射机制

    Go中的反射机制 反射是什么东西? 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力. 在Go 语言中,提供了一种机制在运行时更新变量和检查它 ...

  9. Effective Java之接口优先于反射机制(五十三)

    核心反射机制java.lang.reflect提供了"通过程序来访问关于已装载的类的信息"的能力,给定一个Class实例,可以获得Constructor.Method.Field实 ...

  10. java 操作属性值_java反射机制根据属性名获取属性值的操作

    一.考虑安全访问范围内的属性,没有权限访问到的属性不读取 /** * 根据属性名获取属性值 * * @param fieldName * @param object * @return */ priv ...

最新文章

  1. Zookeeper C API 指南一(转)
  2. 干就行了!!!写程序就像珊瑚,分支太多,哪有那么多复用!
  3. ITK:Voronoi图
  4. c#保存数据格式为.cvs_C#读取csv格式文件的方法
  5. Socket网络编程--小小网盘程序(3)
  6. mongodb在aggregate lookup 进行分页查询,获得记录总数
  7. AOE网的关键路径的计算
  8. QML笔记-KeyNavigation的使用(2种例子)
  9. 优化更新语句中的标量子查询
  10. 为什么在Android上的某些设备上使用相机意图捕获的图像会被旋转?
  11. 阿克曼函数的c语言,C语言,关于阿克曼函数非递归实现的一点拙见
  12. 锁存器、触发器和寄存器
  13. Python遇到的问题:IndentationError: expected an indented block
  14. 我国IPTV研究目前已达到全球领先水平
  15. linux卸载小企鹅输入法,linux下小企鹅输入法的安装
  16. Processing交互应用——躲避炸弹
  17. java微信多客服_怎么实现微信多公众号管理?有哪些多客服系统?
  18. 解决Google 云端硬盘,文件下载问题
  19. Native Instruments Guitar Rig 5 Player WiN-MAC 免费的电吉他效果器
  20. 遍历操作__getitem__

热门文章

  1. 转:visio 2013 激活软件 -- 记录
  2. ignore的音标_单词ignore的音标_词典解释_翻译_相关例句_一直查
  3. 上海瀚示医药行业中文显示电子拣货标签 — 智能播种车
  4. 微信小程序的点击、双击、长按事件
  5. 微信小程序开发之十 —— 点击事件
  6. matlab程序 直线插补,用Matlab实现直线插补计算程序.doc
  7. python生词本查单词译文_Kindle 阅读器“生词本”功能详细使用说明
  8. 电脑和开发板如何串口连接
  9. Ubuntu vim 插件配置
  10. 更新!2021智能仓储物流之最全AGV企业供应商名录