Android应用程序模拟手机按键
记得以前在做一个C++项目时,需要在某一步操作之后人为用代码模拟敲键盘上的回车键(Enter)效果。
出于好奇,这几天研究了一下Android中手机(或平板)上各种按键的键值、模拟方法及最终效果。
1、先来看看Android中对按键和值的定义方式:
public static final int KEYCODE_UNKNOWN = 0;
/** Key code constant: Soft Left key. */
public static final int KEYCODE_SOFT_LEFT = 1;
/** Key code constant: Soft Right key. */
public static final int KEYCODE_SOFT_RIGHT = 2;
/** Key code constant: Home key. */
public static final int KEYCODE_HOME = 3;
/** Key code constant: Back key. */
public static final int KEYCODE_BACK = 4;
/** Key code constant: Call key. */
public static final int KEYCODE_CALL = 5;
/** Key code constant: End Call key. */
public static final int KEYCODE_ENDCALL = 6;
/** Key code constant: '0' key. */
public static final int KEYCODE_0 = 7;
/** Key code constant: '1' key. */
public static final int KEYCODE_1 = 8;
/** Key code constant: '2' key. */
public static final int KEYCODE_2 = 9;
/** Key code constant: '3' key. */
public static final int KEYCODE_3 = 10;
/** Key code constant: '4' key. */
public static final int KEYCODE_4 = 11;
/** Key code constant: '5' key. */
public static final int KEYCODE_5 = 12;
/** Key code constant: '6' key. */
public static final int KEYCODE_6 = 13;
/** Key code constant: '7' key. */
public static final int KEYCODE_7 = 14;
/** Key code constant: '8' key. */
public static final int KEYCODE_8 = 15;
/** Key code constant: '9' key. */
public static final int KEYCODE_9 = 16;
/** Key code constant: '*' key. */
public static final int KEYCODE_STAR = 17;
/** Key code constant: '#' key. */
public static final int KEYCODE_POUND = 18;
/** Key code constant: Directional Pad Up key.
* May also be synthesized from trackball motions. */
public static final int KEYCODE_DPAD_UP = 19;
/** Key code constant: Directional Pad Down key.
* May also be synthesized from trackball motions. */
public static final int KEYCODE_DPAD_DOWN = 20;
/** Key code constant: Directional Pad Left key.
* May also be synthesized from trackball motions. */
public static final int KEYCODE_DPAD_LEFT = 21;
/** Key code constant: Directional Pad Right key.
* May also be synthesized from trackball motions. */
public static final int KEYCODE_DPAD_RIGHT = 22;
/** Key code constant: Directional Pad Center key.
* May also be synthesized from trackball motions. */
public static final int KEYCODE_DPAD_CENTER = 23;
/** Key code constant: Volume Up key.
* Adjusts the speaker volume up. */
public static final int KEYCODE_VOLUME_UP = 24;
/** Key code constant: Volume Down key.
* Adjusts the speaker volume down. */
public static final int KEYCODE_VOLUME_DOWN = 25;
/** Key code constant: Power key. */
public static final int KEYCODE_POWER = 26;
/** Key code constant: Camera key.
* Used to launch a camera application or take pictures. */
public static final int KEYCODE_CAMERA = 27;
/** Key code constant: Clear key. */
public static final int KEYCODE_CLEAR = 28;
/** Key code constant: 'A' key. */
public static final int KEYCODE_A = 29;
/** Key code constant: 'B' key. */
public static final int KEYCODE_B = 30;
/** Key code constant: 'C' key. */
public static final int KEYCODE_C = 31;
/** Key code constant: 'D' key. */
public static final int KEYCODE_D = 32;
/** Key code constant: 'E' key. */
public static final int KEYCODE_E = 33;
/** Key code constant: 'F' key. */
public static final int KEYCODE_F = 34;
/** Key code constant: 'G' key. */
public static final int KEYCODE_G = 35;
/** Key code constant: 'H' key. */
public static final int KEYCODE_H = 36;
/** Key code constant: 'I' key. */
public static final int KEYCODE_I = 37;
/** Key code constant: 'J' key. */
public static final int KEYCODE_J = 38;
/** Key code constant: 'K' key. */
public static final int KEYCODE_K = 39;
/** Key code constant: 'L' key. */
public static final int KEYCODE_L = 40;
/** Key code constant: 'M' key. */
public static final int KEYCODE_M = 41;
/** Key code constant: 'N' key. */
public static final int KEYCODE_N = 42;
/** Key code constant: 'O' key. */
public static final int KEYCODE_O = 43;
/** Key code constant: 'P' key. */
public static final int KEYCODE_P = 44;
/** Key code constant: 'Q' key. */
public static final int KEYCODE_Q = 45;
/** Key code constant: 'R' key. */
public static final int KEYCODE_R = 46;
/** Key code constant: 'S' key. */
public static final int KEYCODE_S = 47;
/** Key code constant: 'T' key. */
public static final int KEYCODE_T = 48;
/** Key code constant: 'U' key. */
public static final int KEYCODE_U = 49;
/** Key code constant: 'V' key. */
public static final int KEYCODE_V = 50;
/** Key code constant: 'W' key. */
public static final int KEYCODE_W = 51;
/** Key code constant: 'X' key. */
public static final int KEYCODE_X = 52;
/** Key code constant: 'Y' key. */
public static final int KEYCODE_Y = 53;
/** Key code constant: 'Z' key. */
public static final int KEYCODE_Z = 54;
其实,在源文件KeyEvent.java中总共定义了将近个键值,这里只给出了个(Back、Home、数字、大写字母等)。
2、而要在程序代码中模拟按键的作用,最好是结合线程一起使用,否则有些按键在模拟过程中会出现程序异常终止的情况。
如返回键Back,键值为,若是直接模拟,就会运行即终止。代码很简单,只需要两句:
Instrumentation inst = new Instrumentation();
inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
Instrumentation和Activity有点类似,只不过Activity是需要一个界面的,而Instrumentation并不是这样的。
我们可以将它理解为一种没有图形界面的,具有启动能力的,用于监控其他类(用Target Package声明)的工具类。
3、接下来,老老实实利用线程(Thread)来实现按键的模拟效果,当然还用到了消息机制(Handler、Message):
a、首先在onCreate()方法之前定义Handler对象handler,
Handler handler;
b、之后在onCreate方法中定义线程并启动,
Thread t = new Thread() {
public void run() {
Looper.prepare();
handler = new Handler() {
public void handleMessage(Message msg) {
Instrumentation inst = new Instrumentation();
//inst.sendCharacterSync(msg.what);
inst.sendKeyDownUpSync(msg.what);
}
};
Looper.loop();
}
};
t.start();
注意上述代码中的注释部分,实践证明两种方法(sendCharacterSync(int keycode),sendKeyDownUpSync(int keycode))都可以达到预期的效果。
c、线程开启了,就差发送带按键值的消息了,在执行代码块中添加以下代码即可,
Message msg = new Message();
//msg.what = KeyEvent.KEYCODE_BACK;
String s = key_value.getText().toString();
if(!("".equals(s)) && s.matches("^[0-9]*$")){
msg.what = Integer.parseInt(s);
handler.sendMessage(msg);
}
else{
Toast.makeText(getApplicationContext(),"please input a right keycde",Toast.LENGTH_SHORT).show();
10 }
这里键值是附带在消息对象的what成员身上的,实现方法很多种,这里不一一给出了。
可以看到,代码中注释部分是将键值固定了:
msg.what = KeyEvent.KEYCODE_BACK;
这样做不管是调试,还是以后运行应用程序来模拟按键,都不太实用,每次都要重新设定并运行程序。
所以,本程序采用后面一种方式:
在主界面上多定义一个编辑框组件key_value,模拟之前输入想要的键值即可(需要将键值由输入的String型转为int型)。
当然,前提是需要对按键与对应的值有个大致的了解,而且,在发送消息之前需要对输入的键值进行空与整型的合法性判断,否则会出现难以察觉的异常。
4、经过测试,发现一些按键是不能通过模拟来达到真实效果的。
如Home键,官方给出的文档显示:Home键不再对一般应用允许模拟,即设置了权限。
和手机全屏截取API类似,需要System级或者Root级应用才可以实现想要的效果。
其所给出的替代方案也行不通:Called to process key events. You can override this to intercept all key events before they are dispatched to the window. Be sure to call this implementation for key events that should be handled normally.
5、下面给出一个比较容易的替代方案,虽然比较山寨,效果还行:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
this.startActivity(intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
需要注意的是,ACTION和CATEGORY的设置和AndroidManifest.xml文件中一致,
标志位设置为Intent.FLAG_ACTIVITY_NEW_TASK,如果不是,则不是以一个新任务的角色生成,会出现问题。
Android应用程序模拟手机按键的更多相关文章
- ADB——模拟手机按键输入
基本命令 adb 模拟按键输入的命令主要通过 input 进行 Usage: input [<source>] <command> [<arg>...] The s ...
- 【python-appium】模拟手机按键搜索异常
执行代码的过程中运行self.driver.press_keycode(84)设备没反映,则需要关闭#desired_caps["unicodeKeyboard"] = " ...
- Android 推断程序在手机中是否是活动状态或者正在执行状态
沈阳斌子在今天项目需求上碰到个这种问题,在Service中须要推断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检測当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页 ...
- Android应用程序键盘(Keyboard)消息处理机制分析
在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形 式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的:在上一篇文章< ...
- 转:Android随笔之——使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)
本文转载自CSDN的jzj1993,原文连接:http://blog.csdn.net/jzj1993/article/details/39158865 有时我们需要使用安卓实现在后台模拟系统按键,比 ...
- Android adb 模拟滑动 按键 点击事件
模拟事件全部是通过input命令来实现的,首先看一下input命令的使用: usage: input ... input text <string> input keyeven ...
- Android随笔之——用shell脚本模拟用户按键、触摸操作
之前写过两篇关于Android中模拟用户操作的博客(其实用一篇是转载的),现在就来讲讲用shell脚本来模拟用户按键操作.本次的目标是用shell脚本打开微信并在其搜索框中搜索相关内容. 本文的模拟功 ...
- Android随笔之——模拟按键操作的几种方式
前几天转过一篇Android上模拟按键操作.触屏事件的博客,昨天又去找了百度.谷歌了一下,写了一点简单的测试代码,留待不时之需.有需要看之前转载的那篇博客的请看这里→_→转:Android随笔之——使 ...
- 解决小米手机不能运行Android Studio程序的问题
转载自:解决小米手机不能运行Android Studio程序的问题 问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: ...
随机推荐
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- Linux下集群的搭建
1.集群的简介: 集群(cluster)技术是一种较新的技术,通过集群技术,可以在付出较低成本的情况下获得在性能.可靠性.灵活性方面的相对较高的收益,其任务调度则是集群系统中的核心技术. 如果一个事情 ...
- noip2008普及组3题题解-rLq
(第一次写题解,随意喷) (只是前一天的作业哈) (先凑个数) 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏.游戏规则是这样的:n个同学站成一个圆圈 ...
- 《TCP/IP详解 卷一》读书笔记-----广播&多播&IGMP
1.广播和多播都只适用于UDP,因为TCP是面向连接的,需要将两台主机的两个进程绑定在一起,即IP地址和端口对 2.通常,网卡能看到网络中的每一个数据帧,但是往往它只接受目的地址与自己MAC地址相同的 ...
- selenium操作滚动条的几种方式
1.操作滚动条到当前可见视图的元素位置 WebElement element = dr.findElement(By.id("4")); ((JavascriptExecutor) ...
- 第2章 面向对象的设计原则(SOLID):2_里氏替换原则(LSP)
2. 里氏替换原则(Liskov Substitution Principle,LSP) 2.1 定义 (1)所有使用基类的地方必须能透明地使用子类替换,而程序的行为没有任何变化(不会产生运行结果错误 ...
- 注册其它地区Apple ID
不同地区Apple Id需求 iPhone 最近换上了iPhone,想要下载其它国家地区的游戏,需要登录该国的Apple Id才能在itunes里下载. 下面记录一下我注册日本地区的Apple Id过 ...
- Android SQLite (四 ) 全面详解(二)
SQLite创建数据库 创建数据库语法: sqlite3 DatabaseName.db 如下展示一个实例: SQLite附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何 ...
- jenkins忘记管理员登陆密码的补救措施
jenkins可以作为我们日常运维过程中代码上线的发版平台,所以对jenkins的安全可靠的维护是十分重要的. 1)在登陆jenkins的时候,如果忘记普通用户的登陆密码,只要能用管理员账号登陆,还可 ...
- Redis集群环境的部署记录
Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施( ...