简体   繁体   English

如何获取前台活动实例?

[英]How to get the foreground activity instance?

I want to create an service app which sends screen-touch events timely to foregound activity. 我想创建一个服务应用程序,该应用程序将屏幕触摸事件及时发送到预兆活动。 The foregound activity is probably a 3pp application. 前程活动可能是3pp应用程序。

My 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);
}

But Service has no dispatchTouchEvent() as Activity has. 但是Service没有Activity那样的dispatchTouchEvent() The only way is to get the foreground activity instance in my service. 唯一的方法是在我的服务中获取前台活动实例。 How to do it? 怎么做? Thanks a lot for your suggestion. 非常感谢您的建议。

You can: 1. Catch the event in your activity A. 2. Send data to activity B. You can use BroadcastReceiver. 您可以:1.在活动A中捕获事件。2.将数据发送到活动B。您可以使用BroadcastReceiver。

If you need to send broadcasts across applications: http://developer.android.com/reference/android/content/BroadcastReceiver.html 如果您需要在应用程序之间发送广播,请访问: http : //developer.android.com/reference/android/content/BroadcastReceiver.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM