How to get the foreground activity instance?

▼魔方 西西 提交于 2019-12-11 16:15:51

问题


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

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. The only way is to get the foreground activity instance in my service. How to do it? Thanks a lot for your suggestion.


回答1:


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

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



来源:https://stackoverflow.com/questions/16783359/how-to-get-the-foreground-activity-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!