我实现了一个大小为1的窗口,希望捕获ACTION_OUTSIDE事件。

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(1,1,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);

我得到触发器,得到ACTION_OUTSIDE事件,但是当读取event.getRawX()event.getRawY()时,它们每次都返回0。
我在android 2.3.6上测试了同样的东西,结果成功了。我找不到任何不赞成的东西。
是Android的问题还是有人知道解决方案?
谢谢

最佳答案

Tniederm,我回答了一个类似的问题here以供参考,但我将在这里用一些小的编辑来重新描述它:
在搜索了源代码之后,我找到了问题的根源:
https://github.com/android/platform_frameworks_base/blob/79e0206ef3203a1842949242e58fa8f3c25eb129/services/input/InputDispatcher.cpp#L1417

// Check whether windows listening for outside touches are owned by the same UID. If it is
// set the policy flag that we will not reveal coordinate information to this window.
if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
    sp<InputWindowHandle> foregroundWindowHandle =
            mTempTouchState.getFirstForegroundWindowHandle();
    const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
    for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
        const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
        if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
            sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle;
            if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) {
                mTempTouchState.addOrUpdateWindow(inputWindowHandle,
                        InputTarget::FLAG_ZERO_COORDS, BitSet32(0));
            }
        }
    }
}

如果“outside touch”落在一个视图中,而该视图不与侦听outside touch的视图共享其uid(read about it here),则事件调度器将其坐标设置为0,0。这绝对是出于安全目的,但我不确定我看到了它旨在减轻的威胁的全部范围。您可以尝试查找较旧版本的inputDispatcher,以了解此功能是什么时候引入的—我还没有查看过自己。
如果你愿意的话,我开了一张关于这个的错误罚单。至少,文档需要包含这些信息…我也想知道这个安全功能是否真的是必要的。
Issue 72746: FLAG_WATCH_OUTSIDE_TOUCH doesn't return location for ACTION_OUTSIDE events on 4.2+

10-08 03:08