Android模拟键盘鼠标事件
来源:互联网 发布:linux找出大文件 编辑:程序博客网 时间:2024/05/20 06:30
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网上有很多成功的范例,此文不再累述,下面着重介绍一下发送键盘鼠标模拟事件的代码:
发送键盘KeyCode:
步骤1. 声明类handler变量
private static Handler handler;
步骤2. 循环处理Message
java代码:
//在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();
步骤3. 在接收到Socket中的传递信息后抛出Message
java代码:
handler.post( new Runnable() {
public void run() {
Instrumentation inst=new Instrumentation();
inst.sendKeyDownUpSync(keyCode);
}
} );
Touch指定坐标,如下例子即
java代码:
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));
- Android模拟键盘鼠标事件
- Android 模拟键盘鼠标事件
- Android模拟键盘鼠标事件
- android 模拟键盘鼠标事件相关
- android 模拟键盘鼠标事件(adb shell)
- android 模拟键盘鼠标事件(adb shell)
- 模拟鼠标键盘事件
- 鼠标键盘事件模拟
- 模拟鼠标、键盘事件
- android 模拟键盘鼠标
- android模拟键盘鼠标
- C#模拟键盘鼠标事件
- C#模拟键盘鼠标事件
- C#模拟键盘鼠标事件
- QTP模拟鼠标键盘事件
- 模拟鼠标键盘操作事件
- Javascript事件模拟(鼠标事件、键盘事件)
- Android 模拟键盘鼠标事件(Socket+Instrumentation实现)
- 项目经理如何把工作简单化
- 推荐一屏幕录制软件:ateditor
- Ecshop 二次开发体会
- ecshop和Jquery的那么点事,解决方案
- jQuery Mobile 1.0 正式版发布
- Android模拟键盘鼠标事件
- 谈谈哲学和人性
- 如何让自己的内心强大起来
- 中国式危机公关9加1策略(第二章 沉默策略)
- 一个程序员的创业历程
- tsal开发文档
- 设计模式之命令模式
- 解决google搜所敏感词汇被重置问题
- JS的数组复制及字符串的相关操作