Android应用程序模拟手机按键

  

  记得以前在做一个C++项目时,需要在某一步操作之后人为用代码模拟敲键盘上的回车键(Enter)效果。

  出于好奇,这几天研究了一下Android中手机(或平板)上各种按键的键值、模拟方法及最终效果。

  1、先来看看Android中对按键和值的定义方式:

  1 public static final int KEYCODE_UNKNOWN         = 0;
  2 /** Key code constant: Soft Left key. */
  3 public static final int KEYCODE_SOFT_LEFT       = 1;
  4 /** Key code constant: Soft Right key. */
  5 public static final int KEYCODE_SOFT_RIGHT      = 2;
  6 /** Key code constant: Home key. */
  7 public static final int KEYCODE_HOME            = 3;
  8 /** Key code constant: Back key. */
  9 public static final int KEYCODE_BACK            = 4;
 10 /** Key code constant: Call key. */
 11 public static final int KEYCODE_CALL            = 5;
 12 /** Key code constant: End Call key. */
 13 public static final int KEYCODE_ENDCALL         = 6;
 14 /** Key code constant: ‘0‘ key. */
 15 public static final int KEYCODE_0               = 7;
 16 /** Key code constant: ‘1‘ key. */
 17 public static final int KEYCODE_1               = 8;
 18 /** Key code constant: ‘2‘ key. */
 19 public static final int KEYCODE_2               = 9;
 20 /** Key code constant: ‘3‘ key. */
 21 public static final int KEYCODE_3               = 10;
 22 /** Key code constant: ‘4‘ key. */
 23 public static final int KEYCODE_4               = 11;
 24 /** Key code constant: ‘5‘ key. */
 25 public static final int KEYCODE_5               = 12;
 26 /** Key code constant: ‘6‘ key. */
 27 public static final int KEYCODE_6               = 13;
 28 /** Key code constant: ‘7‘ key. */
 29 public static final int KEYCODE_7               = 14;
 30 /** Key code constant: ‘8‘ key. */
 31 public static final int KEYCODE_8               = 15;
 32 /** Key code constant: ‘9‘ key. */
 33 public static final int KEYCODE_9               = 16;
 34 /** Key code constant: ‘*‘ key. */
 35 public static final int KEYCODE_STAR            = 17;
 36 /** Key code constant: ‘#‘ key. */
 37 public static final int KEYCODE_POUND           = 18;
 38 /** Key code constant: Directional Pad Up key.
 39  * May also be synthesized from trackball motions. */
 40 public static final int KEYCODE_DPAD_UP         = 19;
 41 /** Key code constant: Directional Pad Down key.
 42  * May also be synthesized from trackball motions. */
 43 public static final int KEYCODE_DPAD_DOWN       = 20;
 44 /** Key code constant: Directional Pad Left key.
 45  * May also be synthesized from trackball motions. */
 46 public static final int KEYCODE_DPAD_LEFT       = 21;
 47 /** Key code constant: Directional Pad Right key.
 48  * May also be synthesized from trackball motions. */
 49 public static final int KEYCODE_DPAD_RIGHT      = 22;
 50 /** Key code constant: Directional Pad Center key.
 51  * May also be synthesized from trackball motions. */
 52 public static final int KEYCODE_DPAD_CENTER     = 23;
 53 /** Key code constant: Volume Up key.
 54  * Adjusts the speaker volume up. */
 55 public static final int KEYCODE_VOLUME_UP       = 24;
 56 /** Key code constant: Volume Down key.
 57  * Adjusts the speaker volume down. */
 58 public static final int KEYCODE_VOLUME_DOWN     = 25;
 59 /** Key code constant: Power key. */
 60 public static final int KEYCODE_POWER           = 26;
 61 /** Key code constant: Camera key.
 62  * Used to launch a camera application or take pictures. */
 63 public static final int KEYCODE_CAMERA          = 27;
 64 /** Key code constant: Clear key. */
 65 public static final int KEYCODE_CLEAR           = 28;
 66 /** Key code constant: ‘A‘ key. */
 67 public static final int KEYCODE_A               = 29;
 68 /** Key code constant: ‘B‘ key. */
 69 public static final int KEYCODE_B               = 30;
 70 /** Key code constant: ‘C‘ key. */
 71 public static final int KEYCODE_C               = 31;
 72 /** Key code constant: ‘D‘ key. */
 73 public static final int KEYCODE_D               = 32;
 74 /** Key code constant: ‘E‘ key. */
 75 public static final int KEYCODE_E               = 33;
 76 /** Key code constant: ‘F‘ key. */
 77 public static final int KEYCODE_F               = 34;
 78 /** Key code constant: ‘G‘ key. */
 79 public static final int KEYCODE_G               = 35;
 80 /** Key code constant: ‘H‘ key. */
 81 public static final int KEYCODE_H               = 36;
 82 /** Key code constant: ‘I‘ key. */
 83 public static final int KEYCODE_I               = 37;
 84 /** Key code constant: ‘J‘ key. */
 85 public static final int KEYCODE_J               = 38;
 86 /** Key code constant: ‘K‘ key. */
 87 public static final int KEYCODE_K               = 39;
 88 /** Key code constant: ‘L‘ key. */
 89 public static final int KEYCODE_L               = 40;
 90 /** Key code constant: ‘M‘ key. */
 91 public static final int KEYCODE_M               = 41;
 92 /** Key code constant: ‘N‘ key. */
 93 public static final int KEYCODE_N               = 42;
 94 /** Key code constant: ‘O‘ key. */
 95 public static final int KEYCODE_O               = 43;
 96 /** Key code constant: ‘P‘ key. */
 97 public static final int KEYCODE_P               = 44;
 98 /** Key code constant: ‘Q‘ key. */
 99 public static final int KEYCODE_Q               = 45;
100 /** Key code constant: ‘R‘ key. */
101 public static final int KEYCODE_R               = 46;
102 /** Key code constant: ‘S‘ key. */
103 public static final int KEYCODE_S               = 47;
104 /** Key code constant: ‘T‘ key. */
105 public static final int KEYCODE_T               = 48;
106 /** Key code constant: ‘U‘ key. */
107 public static final int KEYCODE_U               = 49;
108 /** Key code constant: ‘V‘ key. */
109 public static final int KEYCODE_V               = 50;
110 /** Key code constant: ‘W‘ key. */
111 public static final int KEYCODE_W               = 51;
112 /** Key code constant: ‘X‘ key. */
113 public static final int KEYCODE_X               = 52;
114 /** Key code constant: ‘Y‘ key. */
115 public static final int KEYCODE_Y               = 53;
116 /** Key code constant: ‘Z‘ key. */
117 public static final int KEYCODE_Z               = 54;

  其实,在源文件KeyEvent.java中总共定义了将近260个键值,这里只给出了27个(BackHome数字大写字母等)。

  2、而要在程序代码中模拟按键的作用,最好是结合线程一起使用,否则有些按键在模拟过程中会出现程序异常终止的情况。

  如返回键Back,键值为4,若是直接模拟,就会运行即终止。代码很简单,只需要两句:

1 Instrumentation inst = new Instrumentation();
2 inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);

  InstrumentationActivity有点类似,只不过Activity是需要一个界面的,而Instrumentation并不是这样的。

  我们可以将它理解为一种没有图形界面的,具有启动能力的,用于监控其他类(用Target Package声明)的工具类。

  3、接下来,老老实实利用线程(Thread)来实现按键的模拟效果,当然还用到了消息机制(HandlerMessage):

  a、首先在onCreate()方法之前定义Handler对象handler

1 Handler handler;

  b、之后在onCreate方法中定义线程并启动,

 1 Thread t = new Thread() {
 2     public void run() {
 3         Looper.prepare();
 4         handler = new Handler() {
 5           public void handleMessage(Message msg) {
 6                Instrumentation inst = new Instrumentation();
 7                //inst.sendCharacterSync(msg.what);
 8                inst.sendKeyDownUpSync(msg.what);
 9            }
10         };
11         Looper.loop();
12     }
13 };
14 t.start();

  注意上述代码中的注释部分,实践证明两种方法(sendCharacterSync(int keycode)sendKeyDownUpSync(int keycode))都可以达到预期的效果。

  c、线程开启了,就差发送带按键值的消息了,在执行代码块中添加以下代码即可,

1 Message msg = new Message();
2 //msg.what = KeyEvent.KEYCODE_BACK;
3 msg.what = Integer.parseInt(key_value.getText().toString());
4 handler.sendMessage(msg);

  这里键值是附带在消息对象的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、下面给出一个比较容易的替代方案,虽然比较山寨,效果还行:

 1 @Override
 2 public boolean onKeyDown(int keyCode, KeyEvent event) {
 3     if(keyCode == KeyEvent.KEYCODE_BACK){
 4         Intent intent = new Intent(Intent.ACTION_MAIN);
 5         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 6         intent.addCategory(Intent.CATEGORY_HOME);
 7         this.startActivity(intent);
 8         return true;
 9     }
10     return super.onKeyDown(keyCode, event);
11 }

  需要注意的是,ACTIONCATEGORY的设置和AndroidManifest.xml文件中一致,

  标志位设置为Intent.FLAG_ACTIVITY_NEW_TASK,如果不是,则不是以一个新任务的角色生成,会出现问题。

时间: 2024-08-04 21:09:27

Android应用程序模拟手机按键的相关文章

Android 推断程序在手机中是否是活动状态或者正在执行状态

沈阳斌子在今天项目需求上碰到个这种问题,在Service中须要推断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检測当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页面进行不同的操作. 以下就粘贴出我封装了一下的方法和大家共享. /** * * @Description : 这个包名的程序是否在执行 * @Method_Name : isRunningApp * @param context 上下文 * @param packageName 推断程序的包名 *

Android 判断程序在手机中是否是活动状态或者正在运行状态

沈阳斌子在今天项目需求上碰到个这样的问题,在Service中需要判断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检测当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页面进行不同的操作.下面就粘贴出我封装了一下的方法和大家共享. /** * * @Description : 这个包名的程序是否在运行 * @Method_Name : isRunningApp * @param context 上下文 * @param packageName 判断程序的包名 *

Android应用程序键盘(Keyboard)消息处理机制分析

在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形 式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的:在上一篇文章<Android应用程序消息处理机制(Looper. Handler)分析>中,我们分析了Android应用程序的消息处理机制,本文将结合这种消息处理机制来详细分析Android应用程序是如何获得键 盘按键消息的. 在系统启动的时候,SystemServer会启动窗口管理服务WindowManagerSe

转:Android随笔之——使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)

本文转载自CSDN的jzj1993,原文连接:http://blog.csdn.net/jzj1993/article/details/39158865 有时我们需要使用安卓实现在后台模拟系统按键,比如对音量进行调节(模拟音量键),关闭前台正在运行的App(模拟返回键),或者模拟触屏事件.但是对于原生安卓系统而言,后台进程关闭前台进程,甚至模拟用户事件,进而操控整个系统,是不符合系统安全原则的,如果有这样的漏洞被病毒或恶意软件所利用,会非常危险. 由于一些特殊原因,我恰巧需要实现这样的功能,而又

Android adb 模拟滑动 按键 点击事件

extends:http://blog.csdn.net/huiguixian/article/details/11925389 , http://blog.csdn.net/huiguixian/article/details/8550170 模拟事件全部是通过input命令来实现的,首先看一下input命令的使用: usage: input ... input text <string>       input keyevent <key code number or name>

小米手机不能直接运行Android Studio程序

转载自:http://www.jianshu.com/p/6588c69b42cf Problem description: Android Studio升级到2.3版本之后,小米手机MIUI不能直接点击Run运行Android Studio程序,报如下错误: Installation failed with message Failed to establish session.It is possible that this issue is resolved by uninstalling

解决小米手机不能运行Android Studio程序的问题

转载自:解决小米手机不能运行Android Studio程序的问题 问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message Failed to establish session. It is possible that this issue is resolved by uninstalling an existing version of apk i

android之模拟手机助手下载

在学习了javaSE过后,现在开始继续我的android生活,之前学习完JavaSE,通过UI界面做过模拟游戏<贪吃蛇>和基本版计算器,现在学习Android接近有20天的时间,开始制作模拟手机助手下载,制作手机助手下载,关键是练习一下listview,和listview一些相关的优化,以及优化所产生的一些bug和解决. 本次需呀用到hfs外部软件,来解决网络的问题,和一些图片,用于下载时候的显示.例如手机qq.微信.等一些软件, 下载软件的时候需要下载是时间,因为我们的main主线程不能做耗

Android -- 程序判断手机ROOT状态,获取ROOT权限

判断手机是否具有ROOT权限                                                            /** * 判断手机是否ROOT */ public boolean isRoot() { boolean root = false; try { if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exi