如何获得前台活动实例? [英] How to get the foreground activity instance?

查看:120
本文介绍了如何获得前台活动实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个发送屏幕触摸事件及时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

这篇关于如何获得前台活动实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆