Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an Android app that can capture the global touch screen events when user is playing with another app?

Tags:

android

As the title said, in my research I need to log user touch screen events, mainly gestures(such as tap, scroll, swipe, etc) when a user is using another app. Currently I can't figure out a good way of doing it. It seems that for Android 4.x the touch screen events can not be read out? Anyone knows any good methods to capture the touch events?

like image 722
ppsun Avatar asked Mar 19 '23 20:03

ppsun


2 Answers

I was also doing R&D from quite some time to get global touch events inside an ANDROID App.

Actually our requirement was to capture touch coordinates on phone from inside ANDROID app.

We tried several approaches:

1- First we tried to put a transparent layout(like a layer of glass on above the phone screen) which covers the phone screen and then receive the touch events and dynamically removing the glass so that the event could be passed to below the glass and then dynamically inserting the glass again. But the drawback was that it require two times tap which is not feasible (because 1st tap would give the touch coordinate and second touch would be passed below the glass).

2- Then we tried to use one special flag in window manager i.e. flag_watch_outside_touch by making the transparent layout size 1 X 1 but we were getting touch coordinates as (0,0) because Android Framework imposes security that no other process can get touch points inside other process.

3- Then we tried to run command "adb shell getevents" through code but it was not accepting touch events (although by using "adb shell sendevent" we were able to se send global touch events i.e. by passing a coordinate we were able to pass event on particular coordinate on the device screen.

4- Now we were quite sure that without rooting no App can get global touch events that will happen on some other app process. Android security will not allow this otherwise any app can passwords or other user input data.

But.... we have to do this (without rooting) ...

5- Now we tried to use shell script and executed the getevent command from command prompt and redirected the touch events to a file and parsed the touch events to get readable coordinates ..

So we cannot capture global touchevents inside an app through code but yes we can capture from OS level by executing getevent command from adb shell. That we can the pass to android app by storing it into external storage inside phone storage .

Hope this will help you all.

Thanks,

like image 161
amarnathpatel Avatar answered Apr 05 '23 22:04

amarnathpatel


This is not possible due to security and privacy reasons (as Sir SC mentioned) unless you're rooted.

I have tried multiple things (even asked a question on Stackoverflow: Android - Inactivity/Activity regardless of top app). I came to the conclusion that using an "Accessibility Service" is the closest we can come to knowing when a user has touched the screen. This isn't fool proof, however. You will not get an event for every screen touch (scrolling in Chrome didn't yield any events).


With that said, if your application can rely on a rooted solution then it's possible to listen to incoming lines from getevent (https://source.android.com/devices/tech/input/getevent.html). These lines simply give details of touch (and other) events. But this requires root access so it might not be an acceptable solution.

like image 43
Randy Avatar answered Apr 05 '23 22:04

Randy