获取当前正在运行的活动实例? [英] Get instance of currently running activity?

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

问题描述

我所试图做的是调度移动事件到当前正在运行的活动。我已经得到了当前活动的单元名从这个code

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?

推荐答案

哇,有这么多事情不对的问题,这是很难知道从哪里开始!让我们把它们依次是:

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.

我已经拿到了当前活动的从这个code中的组件名

I have got the ComponentName of the current activity from this code

ActivityManager.getRunningTasks()是的没有的正常应用程序的开发。这适用于像任务管理器和等。你不应该写内核程序逻辑,有依赖于这个函数返回的信息。这是自找麻烦,而事实上我可以保证这样的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?)

我要指派该事件

您真的,真的不应该抓取动作事件出一个窗口,并将其在另一馅。各种调度状态将不会被正确设置,状态的事件将不会是一致的(该事件是设置了原点而这种原始的窗口,而不是新的在你馅吧)等,这是另一种伟大的方式来使一个片状的应用程序,有突破,在未来,如果你甚至可以杂牌它,使其在所有今天的工作的一个很好的机会。

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).

我认为你需要备份的开始,看看你实际上是试图完成​​的任务,得到帮助什么接近的一种合理的方式是。你已经得到了你自己失望的路径是在这一点上pretty的终端。

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.

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

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