本文转自:http://hi.baidu.com/zhouhanqing/blog/item/1bfbaec593f4b5a48326acc7.html
在http://blog.csdn.net/ericahdu 的帮助下,实现了模拟按键,在此,首先表示感谢:)
在源码下写程序可以摆脱SDK的限制,毕竟SDK开放的API有限,比如我们实现模拟按键时,需要用到IWindowManager这个类,但是SDK中是不提供这个类的
首先下载编译源码,然后在源码的frameworks/base/cmds下新建一个文件夹作为你新扩展模块的一个目录。比如叫做autotest,
在autotest下创建一个java文件,比如AutoTest.java。编写你的程序代码,在此你可以使用IWindowManager类,在此,我模拟了按键key,长按键keypress,点触笔touch,点触笔长按touchpress,以及移动move等,代码如下:
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.IWindowManager;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.RemoteException;
import android.util.Log;
public class AutoTest {
public static void main(String args[])throws Exception{
String[] mArgs = args;
try
{
String opt = mArgs[0];
if(opt.equals("touch")){
float x = Float.valueOf(mArgs[1]);
float y = Float.valueOf(mArgs[2]);
MotionEvent e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0);
sendPointerSync(e);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0);
sendPointerSync(e);
}
else if(opt.equals("move")){
float x = Float.valueOf(mArgs[1]);
float y = Float.valueOf(mArgs[2]);
float x2 = Float.valueOf(mArgs[3]);
float y2 = Float.valueOf(mArgs[4]);
MotionEvent e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0);
sendPointerSync(e);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0);
sendPointerSync(e);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0);
sendPointerSync(e);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x2, y2, 0);
sendPointerSync(e);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x2, y2, 0);
sendPointerSync(e);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x2, y2, 0);
sendPointerSync(e);
}
else if(opt.equals("key")){
int keycode = Integer.valueOf(mArgs[1]);
KeyEvent k = new KeyEvent(KeyEvent.ACTION_DOWN,keycode);
sendKeySync(k);
k = new KeyEvent(KeyEvent.ACTION_UP,keycode);
sendKeySync(k);
}
else if(opt.equals("wait")){
int millsecond = Integer.valueOf(mArgs[1]);
Thread.sleep(millsecond);
}
else if(opt.equals("keypress")){
int keycode = Integer.valueOf(mArgs[1]);
int millsecond = Integer.valueOf(mArgs[2]);
KeyEvent k = new KeyEvent(KeyEvent.ACTION_DOWN,keycode);
sendKeySync(k);
Thread.sleep(millsecond);
k = new KeyEvent(KeyEvent.ACTION_UP,keycode);
sendKeySync(k);
}
else if(opt.equals("touchpress")){
float x = Float.valueOf(mArgs[1]);
float y = Float.valueOf(mArgs[2]);
int millsecond = Integer.valueOf(mArgs[3]);
MotionEvent e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0);
sendPointerSync(e);
Thread.sleep(millsecond);
e = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0);
sendPointerSync(e);
}
else System.err.println("** Error: Unknown option: " + opt);
}
catch (RuntimeException ex){}
Thread.sleep(2000);
}
private static void sendPointerSync(MotionEvent event) {
try {
(IWindowManager.Stub.asInterface(ServiceManager.getService("window"))).injectPointerEvent(event, true);
} catch (RemoteException e) {}
}
private static void sendKeySync(KeyEvent event) {
try {
(IWindowManager.Stub.asInterface(ServiceManager.getService("window"))).injectKeyEvent(event, true);
} catch (RemoteException e) {}
}
}
在与java文件同级创建Android.mk文件,内容如下:
# Copyright 2008 The Android Open Source Project
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := autotest
include $(BUILD_JAVA_LIBRARY)
在终端命令行下进入autotest文件夹,输入mm命令,如果报错,则返回Android源码主目录,输入如下命令:
. build/envsetup.sh
此时再返回你的工程目录输入mm就可以了,编译生成一个.jar文件,位于源码的/out/target/product/generic/system/framework下
将编译好的.jar文件放在设备的/system/framework下,新建一个文件,名称为autotest,内容如下:
# Script to start "monkey" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/autotest.jar
exec app_process $base/bin AutoTest $*
将autotest文件放在/system/bin下,用chmod修改文件属性(777)
这样你可以在shell下调用你的.jar文件了
比如输入 shell autotest key 24,向系统注入了调节音量的按键事件。