Get instance of currently running activity?

后端 未结 1 417
别那么骄傲
别那么骄傲 2021-01-05 05:37

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

Ac         


        
1条回答
  •  有刺的猬
    2021-01-05 06:29

    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.

    0 讨论(0)
提交回复
热议问题