What i am trying to do is to dispatch the motion event to currently running activity . I have got the ComponentName of the current activity from this code
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::" +
taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
Now that I have the component name, I want to dispatch the event
dispatchTouchEvent(MotionEvent ev)
that is in the Activity
class, and to do this I need an instance of the current activity. I am stuck at component name. How can I get the Activity
instance so that I can dispatch the event?
You can also set it in the onCreate() method, just set up the variable at the top of your main activity class like this public static Activity activity; then in the onCreate() method just add activity = this; anywhere.
Android called “CurrentActivity”. Then if you want to access the current activity anywhere in your Android application, library, or plugin simply call: CrossCurrentActivity. Current. Activity and you will have the current activity.
The “this” Keyword The this keyword in general sense refers to current class instance. So, when use “this” keyword inside an Activity, it refers to that Activity instance. And as Activity is subclass of “Context”, you will get context of that activity.
In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not.
Wow, there are so many things wrong with this question it is hard to know where to start! Let's take them in order:
What i am trying to do is to dispatch the motion event to currently running activity
The current foreground activity will get motion events, unless it should not. The only way I can imagine this making any sense is that your service has put a system window up that is going on top of all the apps... and in that case, I would urge you to not do this and just let your foreground activity handle the event.
I have got the ComponentName of the current activity from this code
ActivityManager.getRunningTasks() is not for normal application development. This is intended for things like task managers and such. You should never write core program logic that has dependencies on the information returned by this function. That is begging for trouble, and in fact I can guarantee such code will break at some point. (What happens when multiple applications can be running in the front at the same time?)
I want to dispatch the event
You really, really should not be ripping motion events out of one window and stuffing them in to another. Various dispatch state will not be set up correctly, state in the event will not be consistent (the event was set up with an origin and such for the original window, not the new one you are stuffing it in), etc. This is another great way to make a flaky application that has a good chance of breaking in the future, if you can even kludge it to make it work at all today.
I am stuck at component name. How can I get the Activity instance so that I can dispatch the event?
Indeed, all you have is a component name. That has nothing to do with active instances. There is no magic way to turn this into an actual instance. It would be wrong to supply one, because this can easily be ambiguous (if there were two instances of that class instantiated).
I think you need to back-up to the start and look at what you are actually trying to accomplish, to get help on what a reasonable way of approaching it is. The path you have gotten yourself down is at this point pretty terminal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With