问题
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