如何在Android中发送指针事件

我正在尝试检测Android中的虚拟键盘高度。

我发现了一个类似的话题:获取Android中虚拟键盘的高度

看来作者找到了一种检测身高的方法:

我找到了一种方法来获得它。 在我请求打开虚拟键盘之后,我发送了我生成的指针事件。 它们的y坐标从设备的高度开始并减小。

我不明白该怎么做。


我将使用您发布的链接提供的代码:

// Declare Variables

int softkeyboard_height = 0;
boolean calculated_keyboard_height;
Instrumentation instrumentation;

// Initialize instrumentation sometime before starting the thread
instrumentation = new Instrumentation();

mainScreenView是您的基本视图,您的活动的视图。 m (ACTION_DOWN)和m1 (ACTION_UP)是使用Instrumentation#sendPointerSync(MotionEvent) MotionEvent)分派的触摸事件。 逻辑是,调度到键盘显示位置的MotionEvent将导致以下SecurityException

java.lang.SecurityException:注入到另一个应用程序需要INJECT_EVENTS权限

所以,我们从屏幕的底部开始,并在循环的每次迭代中向上(通过递减y )。 对于一定数量的迭代,我们将得到一个SecurityException(我们将捕获它):这意味着MotionEvent发生在键盘上。 当y变得足够小(当它刚好在键盘上方)时,我们将跳出循环并使用以下公式计算键盘的高度:

softkeyboard_height = mainScreenView.getHeight() - y;

码:

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)

分派指针事件。 在收件人从事件处理返回后的某一时刻完成,尽管它可能没有完全结束事件的反应 - 例如,如果它需要更新其显示的结果,它可能仍然处于正在处理的过程中那。


使用OnGlobalLayoutListener,它对我来说非常合适。

链接地址: http://www.djcxy.com/p/56823.html

上一篇: How to send out pointer event in Android

下一篇: How to you check the status or kill an external process with python