如何获得前台活动实例?前台、如何获得、实例

2023-09-12 22:27:29 作者:喜丧?

我想创建一个发送屏幕触摸事件及时foregound活动的服务应用程序。该foregound活动可能是一个3PP应用程序。

I want to create an service app which sends screen-touch events timely to foregound activity. The foregound activity is probably a 3pp application.

我的codeS:

public void myClickEvent(float x, float y) {
    long firstTime = SystemClock.uptimeMillis();
    final MotionEvent firstEvent = MotionEvent.obtain(firstTime, firstTime,
            MotionEvent.ACTION_DOWN, x, y, 0);

    long secondTime = firstTime + 100;
    final MotionEvent secondEvent = MotionEvent.obtain(secondTime,
            secondTime, MotionEvent.ACTION_UP, x, y, 0);

    dispatchTouchEvent(firstEvent);
    dispatchTouchEvent(secondEvent);
}

但服务没有 dispatchTouchEvent()的活动了。 唯一的办法是让前台活动实例在我的服务。 怎么做? 非常感谢您的建议。

But Service has no dispatchTouchEvent() as Activity has. The only way is to get the foreground activity instance in my service. How to do it? Thanks a lot for your suggestion.

推荐答案

您可以: 1.抓住事件的活动A. 2.将数据发送到活动B.可以使用的BroadcastReceiver。

You can: 1. Catch the event in your activity A. 2. Send data to activity B. You can use BroadcastReceiver.

如果您需要跨应用程序发送的广播: http://developer.android.com/reference/android/content/BroadcastReceiver.html

If you need to send broadcasts across applications: http://developer.android.com/reference/android/content/BroadcastReceiver.html