How to detect touch events outside the view in background service?

mattyeh :

I was trying to detect touch events in background service so that I can get all the touch events when user is using any apps.

What's I've done is using WindowManager to add a small view and when running the app in the background, this small view can still be on the screen. I also set the view as an onTouchListener so when user touch inside the view, I can get the touch event.

My problem is that is there any way to detect touch events outside this small view.

Here is my code.

public class GlobalTouchService extends Service implements View.OnTouchListener {


private WindowManager mWindowManager;
private WindowManager.LayoutParams mLayoutParams;
private MyView myView;
private boolean flag = true;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    mWindowManager = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    mLayoutParams = new WindowManager.LayoutParams();
    mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    mLayoutParams.format = PixelFormat.TRANSLUCENT;
    mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    mLayoutParams.x = 0;
    mLayoutParams.y = 0;
    mLayoutParams.height = 300;
    mLayoutParams.width = 300;

    myView = new MyView(this);
    myView.setOnTouchListener(this);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (flag) {
        flag = false;
        mWindowManager.addView(myView, mLayoutParams);
    }
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    float x = motionEvent.getRawX();
    float y = motionEvent.getRawY();
    Log.d("x,y", "X" + x + " Y" + y);

    return false;
}
Gabe Sechan :

You can't. Android explicitly does not allow you to get touch events when you aren't the foreground app for security reasons. What you're trying to do is explicitly against what the OS wants, and if you find a way Google will plug the hole as quick as you find it. The only way to do this is on a rooted device, which means it won't work over the play store (and the way to do it there is by reading touch data from the linux device layer).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=183115&siteId=1