如何在Android中发送指针事件 [英] How to send out pointer event in Android
问题描述
我发现一个类似的主题:获取Android中虚拟键盘的高度
似乎作者发现检测高度的方法:
我找到了一种方法来获取它。在我要求打开虚拟键盘之后,我发送
发送我生成的指针事件。他们的y坐标从设备的
高度开始下降。
我不明白如何做到这一点。 p>
我将使用您发布的链接提供的代码:
//声明变量
int softkeyboard_height = 0;
boolean calculate_keyboard_height;
仪器仪表;
//在启动线程之前的某个时间初始化仪器
instrumentation = new Instrumentation();
mainScreenView
是您的基本视图,视图。 (ACTION_DOWN)和 m1
(ACTION_UP)是使用 Instrumentation# sendPointerSync(MotionEvent)
。逻辑是将MotionEvent调度到键盘正在显示的位置将导致以下 SecurityException
:
java.lang.SecurityException:注入另一个应用程序需要
INJECT_EVENTS权限
所以,我们从屏幕的底部,并在循环的每次迭代中使我们的方式(通过递减 y
)。对于某些迭代次数,我们将得到一个SecurityException(我们将会抓到):这将意味着MotionEvent正在键盘上发生。当 y
变得足够小(当它刚刚在键盘上方)时,我们将突破循环,并使用以下方式计算键盘的高度:
softkeyboard_height = mainScreenView.getHeight() - y;
代码:
code> Thread t = new Thread(){
public void run(){
int y = mainScreenView.getHeight() - 2;
int x = 10;
int counter = 0;
int height = y;
while(true){
final MotionEvent m = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,
x,
y,
1);
final MotionEvent m1 = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,
x,
y,
1);
boolean pointer_on_softkeyboard = false;
try {
instrumentation.sendPointerSync(m);
instrumentation.sendPointerSync(m1);
} catch(SecurityException e){
pointer_on_softkeyboard = true;
}
if(!pointer_on_softkeyboard){
if(y == height){
if(counter ++< 100){
Thread.yield();
继续;
}
} else if(y> 0){
softkeyboard_height = mainScreenView.getHeight() - y;
Log.i(,软键盘的高度是:+ softkeyboard_height);
}
break;
}
y--;
}
if(softkeyboard_height> 0){
//计算并保存在softkeyboard_height
} else {
calculated_keyboard_height = false;
}
}
};
t.start();
Instrumentation#sendPointerSync(MotionEvent)
/ p>
调度指针事件。收件人
从事件处理返回后,在某个时候完成,虽然可能没有
完全响应事件 - 例如,如果
需要更新其显示为结果,它可能仍然在
的过程中。
I'm trying to detect the virtual keyboard height in Android.
I found a similar topic: Get the height of virtual keyboard in Android
It seems the author found a way to detect the height:
I found a way to get it. After I request to open virtual keyboard, I send pointer event that I generate. their y coordinate starts from height of device and decreases.
I don't understand how to do that.
I'll be using the code provided at the link that you posted:
// Declare Variables
int softkeyboard_height = 0;
boolean calculated_keyboard_height;
Instrumentation instrumentation;
// Initialize instrumentation sometime before starting the thread
instrumentation = new Instrumentation();
mainScreenView
is your base view, your activity's view. m
(ACTION_DOWN) and m1
(ACTION_UP) are touch events that are dispatched using Instrumentation#sendPointerSync(MotionEvent)
. The logic is that a MotionEvent dispatched to where the keyboard is being displayed will cause the following SecurityException
:
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
So, we start at the bottom of the screen and make our way up (by decrementing y
) on every iteration of the loop. For certain number of iterations, we will get a SecurityException (which we'll catch): this would imply that the MotionEvent is happening over the keyboard. The moment y
gets small enough (when its just above the keyboard), we'll break out of the loop and calculate the keyboard's height using:
softkeyboard_height = mainScreenView.getHeight() - y;
Code:
Thread t = new Thread(){
public void run() {
int y = mainScreenView.getHeight()-2;
int x = 10;
int counter = 0;
int height = y;
while (true){
final MotionEvent m = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,
x,
y,
1);
final MotionEvent m1 = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,
x,
y,
1);
boolean pointer_on_softkeyboard = false;
try {
instrumentation.sendPointerSync(m);
instrumentation.sendPointerSync(m1);
} catch (SecurityException e) {
pointer_on_softkeyboard = true;
}
if (!pointer_on_softkeyboard){
if (y == height){
if (counter++ < 100){
Thread.yield();
continue;
}
} else if (y > 0){
softkeyboard_height = mainScreenView.getHeight() - y;
Log.i("", "Soft Keyboard's height is: " + softkeyboard_height);
}
break;
}
y--;
}
if (softkeyboard_height > 0 ){
// it is calculated and saved in softkeyboard_height
} else {
calculated_keyboard_height = false;
}
}
};
t.start();
Instrumentation#sendPointerSync(MotionEvent)
:
Dispatch a pointer event. Finished at some point after the recipient has returned from its event processing, though it may not have completely finished reacting from the event -- for example, if it needs to update its display as a result, it may still be in the process of doing that.
这篇关于如何在Android中发送指针事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!