https://github.com/zytc2009/BigTeam_learning
分类: 嵌入式
2012-07-05 12:02:40
* Socket编程:实现PC和Emulator通讯,并进行循环监听
* Service服务:将Socket的监听程序放在Service中,从而达到后台运行的目的。这里要说明的是启动服务有两种方 式,bindService和startService,两者的区别是,前者会使启动的Service随着启动Service的Activity的消亡而 消亡,而startService则不会这样,除非显式调用stopService,否则一直会在后台运行因为Service需要通过一个 Activity来进行启动,所以采用startService更适合当前的情形
* Instrumentation发送键盘鼠标事件:Instrumentation提供了丰富的以send开头的函数接口来实现模拟键盘鼠标,如下所述:
sendCharacterSync(int keyCode) //用于发送指定KeyCode的按键
sendKeyDownUpSync(int key) //用于发送指定KeyCode的按键
sendPointerSync(MotionEvent event) //用于模拟Touch
sendStringSync(String text) //用于发送字符串
注意:以上函数必须通过Message的形式抛到Message队列中。如果直接进行调用加会导致程序崩溃。
对于Socket编程和Service网上有很多成功的范例,此文不再累述,下面着重介绍一下发送键盘鼠标模拟事件的代码:
1. 发送键盘KeyCode:
步骤1. 声明类handler变量
private static Handler handler; |
//在Activity的onCreate方法中对下列函数进行调用 private void createMessageHandleThread(){ //need start a thread to raise looper, otherwise it will be blocked Thread t = new Thread() { public void run() { Log.i( TAG,"Creating handler ..." ); Looper.prepare(); handler = new Handler(){ public void handleMessage(Message msg) { //process incoming messages here } }; Looper.loop(); Log.i( TAG, "Looper thread ends" ); } }; t.start(); } |
handler.post( new Runnable() { public void run() { Instrumentation inst=new Instrumentation(); inst.sendKeyDownUpSync(keyCode); } } ); |
2. Touch指定坐标,如下例子即touch point(240,400)
Instrumentation inst=new Instrumentation(); inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 240, 400, 0)); inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 240, 400, 0)); |
3. 模拟滑动轨迹
将上述方法中间添加 MotionEvent.ACTION_MOVE
命令格式2:adb shell sendevent [device] [type] [code] [value]
如: adb shell sendevent /dev/input/event0 1 229 1 代表按下按下menu键
adb shell sendevent /dev/input/event0 1 229 0 代表按下松开menu键
说明:上述的命令需组合使用
另外所知道的命令如下:
Key Name CODE
MENU 229
HOME 102
BACK (back button) 158
CALL (call button) 231
END (end call button) 107
2. 发送鼠标事件(Touch):
命令格式:adb shell sendevent [device] [type] [code] [value]
情况1:在某坐标点上touch
如在屏幕的x坐标为40,y坐标为210的点上touch一下,命令如下
adb shell sendevent /dev/input/event0 3 0 40
adb shell sendevent /dev/input/event0 3 1 210
adb shell sendevent /dev/input/event0 1 330 1 //touch
adb shell sendevent /dev/input/event0 0 0 0 //it must have
adb shell sendevent /dev/input/event0 1 330 0 //untouch
adb shell sendevent /dev/input/event0 0 0 0 //it must have
注:以上六组命令必须配合使用,缺一不可
情况2:模拟滑动轨迹(可下载并采用aPaint软件进行试验)
如下例是在aPaint软件上画出一条开始于(100,200),止于(108,200)的水平直线
adb shell sendevent /dev/input/event0 3 0 100 //start from point (100,200)
adb shell sendevent /dev/input/event0 3 1 200
adb shell sendevent /dev/input/event0 1 330 1 //touch
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 3 0 101 //step to point (101,200)
adb shell sendevent /dev/input/event0 0 0 0
…………………… //must list each step, here just skip
adb shell sendevent /dev/input/event0 3 0 108 //end point(108,200)
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 330 0 //untouch
adb shell sendevent /dev/input/event0 0 0 0