Monitor Screen Touch Event in Android
来源:互联网 发布:java使用的字符码集 编辑:程序博客网 时间:2024/05/16 08:14
http://jhshi.me/2014/11/09/monitor-screen-touch-event-in-android/index.html
In one of my projects I need to track every screen touch event in background.That is, my app needs to be "invisible" while capturing every screen touch. Hereis how I achieved this.
The idea is to define a dummy UI fragment that is really tiny (say, 1x1 pixel),and place it on one of the corners of the screen, and let it listen on all touchevents outside it. Well, literally, it's not "invisible", in fact it's inforeground all the time! But since it's so tiny so hopefully users won't feel adifference.
First, let's create this dummy view:
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);mDummyView = new LinearLayout(mContext);LayoutParams params = new LayoutParams(1, LayoutParams.MATCH_PARENT);mDummyView.setLayoutParams(params);mDummyView.setOnTouchListener(this);
Here we set the width of the dummy view to be 1 pixel, and the height to beparent height. And we also set up a touch event listen of this dummy view, whichwe'll implement later.
Then let's add this dummy view.
LayoutParams params = new LayoutParams( 1, /* width */ 1, /* height */ LayoutParams.TYPE_PHONE, LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSPARENT );params.gravity = Gravity.LEFT | Gravity.TOP;mWindowManager.addView(mDummyView, params);
The key here is the FLAG_WATCH_OUTSIDE_TOUCH
flag, it enables the dummy viewto capture all events on screen, whether or not the event is inside the dummyview or not.
Finally, let's handle the touch event by implementing View.OnTouchListener
listener.
@Overridepublic boolean onTouch(View v, MotionEvent event) { Log.d(TAG, "Touch event: " + event.toString()); // log it return false;}
We need to return false
since we're not really handling the event, so that theunderlying real UI elements can get those events.
A final note is that, to keep our dummy view always listening touch events, weneed to wrap all these in anService
: we create the dummy view in onCreate
and add it to screen inonStartCommand
. And the service should implementView.OnTouchListener
to receive the touch events.
- Monitor Screen Touch Event in Android
- Android Screen Monitor
- Android Screen Monitor
- Android Screen Monitor
- Android Screen Monitor
- Android Screen Monitor
- Android Screen Monitor
- Android Screen Monitor使用
- Android Screen Monitor
- Android screen monitor
- Android screen monitor
- Android Screen Monitor (asm.jar)
- android screen monitor抓取手机屏幕
- Android Screen Monitor 下载地址
- Android Screen Monitor真机抓屏教程
- Android Screen Monitor的使用
- android touch event summary
- HID android add touch screen
- NGUI坑之首行缩进
- 32(64)位CPU vs 32(64)位OS
- testng(二) 美化报告,jenkins集成
- linux服务器被挂马,ps命令netstat命令被挟持替换成其他程序
- mybatis 高级映射和spring整合之与Spring整合(6)
- Monitor Screen Touch Event in Android
- UITabbar UINavigationBar
- 定制Android系统开发之五——ServiceFetcher
- XML AJAX实现responseXML时头文件的申明
- 增加google服务包编译Chrome后原本的Browser.apk不编译问题
- Knockoutjs快速入门
- 【解决】由于没有远程桌面授权服务器可以提供许可证,远程回话被中断
- 图像处理之相似图片识别(直方图应用篇)
- hadoop源码学习积累1