博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4.5节_Android硬件访问服务使用反射
阅读量:4575 次
发布时间:2019-06-08

本文共 8648 字,大约阅读时间需要 28 分钟。

修改编译选项,不包含这两个classes文件

 

 

怎么使用反射访问硬件服务:

 修改代码

 

 

 

 

 

 

 

 

虽然getService返回的是IBinder类,但invoke返回的是Object

Object ledService = getService.invoke(null, "led");
//iLedService = ILedService.Stub.asInterface(ServiceManager.getService("led"));(ServiceManager.getService("led"))--------->对应  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);

 

 设置过滤器,查看打印信息:

 

 

 

 

 

package com.thisway.app_0001_leddemo;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.view.View;import android.widget.CheckBox;import android.widget.Toast;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;//import android.os.ILedService;//import android.os.ServiceManager;import android.os.IBinder;public class MainActivity extends AppCompatActivity {    private boolean ledon = false;    private Button button = null;    private CheckBox checkBoxLed1 = null;    private CheckBox checkBoxLed2 = null;    private CheckBox checkBoxLed3 = null;    private CheckBox checkBoxLed4 = null;    //private ILedService iLedService = null;    Object proxy = null;    Method ledCtrl = null;    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++)                        //iLedService.ledCtrl(i, 1);                        ledCtrl.invoke(proxy, 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++)                        //iLedService.ledCtrl(i, 0);                        ledCtrl.invoke(proxy, 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();        try {            // Check which checkbox was clicked            switch(view.getId()) {                case R.id.LED1:                    if (checked) {                        // Put some meat on the sandwich                        Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(0, 1);                        ledCtrl.invoke(proxy, 0, 1);                    }                    else {                        // Remove the meat                        Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(0, 0);                        ledCtrl.invoke(proxy, 0, 0);                    }                    break;                case R.id.LED2:                    if (checked) {                        // Put some meat on the sandwich                        Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(1, 1);                        ledCtrl.invoke(proxy, 1, 1);                    }                    else {                        // Remove the meat                        Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(1, 0);                        ledCtrl.invoke(proxy, 1, 0);                    }                    break;                case R.id.LED3:                    if (checked) {                        // Put some meat on the sandwich                        Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(2, 1);                        ledCtrl.invoke(proxy, 2, 1);                    }                    else {                        // Remove the meat                        Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(2, 0);                        ledCtrl.invoke(proxy, 2, 0);                    }                    break;                case R.id.LED4:                    if (checked) {                        // Put some meat on the sandwich                        Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(3, 1);                        ledCtrl.invoke(proxy, 3, 1);                    }                    else {                        // Remove the meat                        Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();                        //iLedService.ledCtrl(3, 0);                        ledCtrl.invoke(proxy, 3, 0);                    }                    break;                // TODO: Veggie sandwich            }        } 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 {            //iLedService = ILedService.Stub.asInterface(ServiceManager.getService("led"));            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());/*        button.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                // Perform action on click                ledon = !ledon;                if (ledon)                    button.setText("ALL OFF");                else                    button.setText("ALL ON");            }        });*/    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

 

转载于:https://www.cnblogs.com/zhulinhaibao/p/7020284.html

你可能感兴趣的文章
Altium Designer 中差分走线
查看>>
linux 解压缩命令
查看>>
GDUT校赛
查看>>
递归方程组解的渐进阶的求法——差分方程法
查看>>
(HDU)1076 --An Easy Task(简单任务)
查看>>
团队精神与集体主义的区别?
查看>>
Spring Boot 入门(Spring Cloud方向)
查看>>
仿淘宝商品图片放大镜效果(鼠标移动上去会出现放大的图片,并且可以移动)...
查看>>
AngularJS(九):路由
查看>>
Google chrome浏览器HTML5 Beta项目, 未来Web前瞻!
查看>>
GPS.NET 和 GeoFramework开源了
查看>>
汇编:采用址表的方法编写程序实现C程序的switch功能
查看>>
AtiveMQ初次连接的 http error:503 连接错误 Prolem accessing /.Reason : Service Unavailable...
查看>>
Lua1.1 Lua 的参考手册 (三)
查看>>
OFO和摩拜共享单车
查看>>
Linux软件安装管理之1——rpm命令管理
查看>>
visual studio 2017 使用笔记
查看>>
iTerm2 半透明颜色主题与字体配置
查看>>
《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集...
查看>>
asp.net后台InputStream存储图片,前台js接收解析存放路径地址
查看>>