Android的模拟按键preSS [英] Android simulate key press
本文介绍了Android的模拟按键preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式模拟一个Droid的关键preSS?我想模仿手工密钥preSS(出现在机器人某人是pressing的关键,但它正在做编程)。
How can I programmatically simulate a key press on a Droid? I would like to mimic a manual key press (appearing on the droid that someone is pressing a key but it is being done programmatically).
有解决方案在那里,涉及 IWindowManager
,但不是在新的SDK了一种选择。
There are solutions out there involving IWindowManager
, but that isn't an option anymore in the new SDK.
推荐答案
您可以用仪表,也就是后续code,从您的活动的onCreate呼吁将导致打开的菜单和关闭多次:
You can use instrumentation, ie following code called from onCreate of your activity will cause menu to be opened and closed multiple times:
new Thread(new Runnable() {
@Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
for ( int i = 0; i < 10; ++i ) {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
Thread.sleep(2000);
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Thread.sleep(2000);
}
}
catch(InterruptedException e){
}
}
}).start();
...但我不知道,如果这就是你以后
...but I am not sure if this is what you are after
这篇关于Android的模拟按键preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文