Shane Duffy
Shane Duffy

Reputation: 1147

How can I detect click events of external application in Android?

I'm currently attempting to make an AI that will be able to learn how to play games on Android by actively monitoring certain features pertaining to the pixels on the screen, and how the user interacts with them.

My issue is that I cannot seem to find any relevant information regarding detecting MotionEvents that other applications receive. Are there any standard means by which I could set a global OnTouchEvent hook, thus receiving all user inputs regardless of the application that is active? If there aren't any standard methods, any ideas as to how one could achieve this?

Upvotes: 1

Views: 1989

Answers (2)

Sachin Chauhan
Sachin Chauhan

Reputation: 366

You can detect external touch event in your application. WINDOWS_SERVICE which is system service, can provide you with the information of touch on the Android device, for that you need one layout which should be of unit size of pixel so that it doesn't consumes you click event on external applications.

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
1, // width is equal to 1px
1, // height is equal to 1px
WindowManager.LayoutParams.TYPE_PHONE, // Type Phone, These are non-application windows providing user interaction with the phone
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | // This window would never get key input focus.
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, // This window will get outside touch.
PixelFormat.TRANSPARENT // The view will be transparent
);

Now you have to add you layout to this window manager service.

//Adding view to the window manager
mWindowManager.addView(touchLayout, params);

For better explanation with code you can refer this link http://allinmyspace.com/2016/08/28/android-detect-touch-events-on-external-applications/

Upvotes: 3

Doug Stevenson
Doug Stevenson

Reputation: 317692

One application can not know anything that is going on inside another application unless that other app has specifically shared that data. This is for security and privacy reasons. (Imagine how unsafe phones would be if any app could know what the user is entering into any other app, such as password.)

Upvotes: 4

Related Questions