Android 如何处理屏幕所有触摸、点击事件?

qq_20942245 2016-06-03 12:07:17
有个需求,要求在10秒钟内如果没有任何操作的话,就跳转到指定的一个Activity。
现在的问题是,我重写Ontouch 方法就只监听得到屏幕空白的地方,点击控件触发不了,
我想在ontouch里完成记录屏幕触摸的事件(包括空白地方和控件的操作),怎么处理?
...全文
2192 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
王能 2019-10-25
  • 打赏
  • 举报
回复
引用 4 楼 lisuixin 的回复:
不去监听onTouchEvent方法,而是监听dispatchTouchEvent的方法有效果。
勿挖坟 重写Activity的dispatchTouchEvent,即可

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mHandler.removeCallbacksAndMessages(null);
        mHandler.sendEmptyMessageDelayed(0, 10_000);
        return super.dispatchTouchEvent(ev);
    }
handler里面的逻辑就是跳转新界面
失落夏天 版主 2019-10-17
  • 打赏
  • 举报
回复
几个思路吧,你可以先看看我之前写的一篇文章,事件分发机制,看看哪里可以hook掉,设置代理类替换掉原有的类。
https://blog.csdn.net/rzleilei/article/details/53055370

下面只是我的一些思路,没有去尝试实现过。
思路一:事件分发会经过activity的dispatchTouchEvent方法,基类activity中重写这个方法监听就好了。缺点:作为支持库就不行了。
思路二:所有的事件分发其实都会先通过native层,在native层做一个hook。我们之前做app爬虫就用的这个方案。缺点:做起来很麻烦,估计你没那精力去做
思路三:点击的话其实最终都通过handler传递到最终的clicklistener,给MainLooper设置给Printer,判断Printer输出是否是事件传递的hander。缺点:仅支持点击事件
lisuixin 2019-10-17
  • 打赏
  • 举报
回复
不去监听onTouchEvent方法,而是监听dispatchTouchEvent的方法有效果。
ganshenml 2016-06-05
  • 打赏
  • 举报
回复
先了解下事件分发拦截机制,将父控件对事件的处理进行拦截后就不会传递到子控件。比如子控件全部被包裹在父控件LinearLayout中且所有触摸事件被其拦截,那么问题就解决了
睡精灵 2016-06-04
  • 打赏
  • 举报
回复
重写dispatchTouchEvent这个方式试试
蒲锦_up 2016-06-03
  • 打赏
  • 举报
回复


/**
* Used by custom windows, such as Dialog, to pass the touch screen event
* further down the view hierarchy. Application developers should
* not need to implement or call this.
*
*/
public abstract boolean superDispatchTouchEvent(MotionEvent event);

/**
* Used by custom windows, such as Dialog, to pass the trackball event
* further down the view hierarchy. Application developers should
* not need to implement or call this.
*
*/
public abstract boolean superDispatchTrackballEvent(MotionEvent event);

/**
* Used by custom windows, such as Dialog, to pass the generic motion event
* further down the view hierarchy. Application developers should
* not need to implement or call this.
*
*/
public abstract boolean superDispatchGenericMotionEvent(MotionEvent event);


试试吧 也许管用!!

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧