Android:在窗口下方的任何应用程序中模拟触摸的后台服务

时间:2022-05-06 01:24:28

I have a Service that can draw a Canvas on top of all applications using a SYSTEM_ALERT_WINDOW which contains a custom View.

我有一个服务,可以使用包含自定义视图的SYSTEM_ALERT_WINDOW在所有应用程序之上绘制Canvas。

My Service and custom View both implement View.OnTouchListener

我的服务和自定义View都实现了View.OnTouchListener

The onTouch(View v, MotionEvent ev){} method of the Service returns true while the custom View returns false. This allows me to display my custom View on top of all apps and still interact with the underlying Activity. This part works.

当自定义View返回false时,Service的onTouch(View v,MotionEvent ev){}方法返回true。这允许我在所有应用程序之上显示我的自定义视图,并仍然与基础活动进行交互。这部分有效。

I also want the Service to simulate a touch event in the specified coordinates in the current Activity that is underneath the SYSTEM_ALERT_WINDOW.

我还希望服务模拟SYSTEM_ALERT_WINDOW下当前Activity中指定坐标中的触摸事件。

I was hoping I could call View.dispatchTouchEvent(...) on the custom View and because onTouch(...) returns false, the touch event would get passed on to the underlying Activity. It does not work.

我希望我可以在自定义视图上调用View.dispatchTouchEvent(...),因为onTouch(...)返回false,触摸事件将传递给基础Activity。这是行不通的。

How can I simulate touch events in any Activity in this situation?

在这种情况下,如何在任何活动中模拟触摸事件?

I have used code from the following sources...

我使用了以下来源的代码......

Draw a canvas on top of all applications:

在所有应用程序之上绘制画布:

http://www.piwai.info/chatheads-basics/

Draw an Android canvas on top of all applications?

在所有应用程序之上绘制Android画布?

Passing touches to underlying app:

将触摸传递给底层应用:

How can I interact with elements behind a translucent Android app?

如何与半透明Android应用程序背后的元素进行交互?

Simulating touches:

How to simulate a touch event in Android?

如何在Android中模拟触摸事件?

1 个解决方案

#1


0  

AFAIK, you can't, for obvious security reasons, except probably on rooted devices. Even the accessibility APIs don't support this AFAICT. Allowing arbitrary apps to fake input to other apps would be a major security hole.

AFAIK,出于明显的安全原因,你不能,除了可能在root设备上。即使是辅助功能API也不支持此AFAICT。允许任意应用程序伪造其他应用程序的输入将是一个主要的安全漏洞。

If your objective is to do this during testing, instrumentation testing allows you to simulate touch events in your own app.

如果您的目标是在测试期间执行此操作,则仪器测试允许您在自己的应用程序中模拟触摸事件。

#1


0  

AFAIK, you can't, for obvious security reasons, except probably on rooted devices. Even the accessibility APIs don't support this AFAICT. Allowing arbitrary apps to fake input to other apps would be a major security hole.

AFAIK,出于明显的安全原因,你不能,除了可能在root设备上。即使是辅助功能API也不支持此AFAICT。允许任意应用程序伪造其他应用程序的输入将是一个主要的安全漏洞。

If your objective is to do this during testing, instrumentation testing allows you to simulate touch events in your own app.

如果您的目标是在测试期间执行此操作,则仪器测试允许您在自己的应用程序中模拟触摸事件。