将数据从 Android 线程传递到未创建该线程的 Activity
我有一个 Android 后台线程,其创建者早已不在了。但线程始终保持运行并记录数据。我想将此数据传递或发送到当前正在运行的未创建线程的活动。
我必须做什么?谷歌?这会带来有关将工作传递到后台的内容,以免阻塞主 UI 线程。那不是我想要的。我想将数据传递/发送到当前正在运行的新活动。
I have an Android background thread whose creator is long gone. But the thread keeps running and logging data all the time. I want to pass or send this data to the current running activity that did not create the thread.
What do I have to do? Google? That brings up stuff about passing work to the background so as to not block the main UI thread. That's not what I want. I want to pass/send the data to the new currently running activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,如果您正在执行此类后台线程,则需要使用服务来保存该线程。在您的 Activity 中,您只需绑定到 Service,当您获取 Service 实例时,您可以向它传递一个 Handler。然后,服务中的线程可以访问 Handler 并通过 Handler 向 Activity 发送消息。当 Activity 完成
unbind()
时,清除 Handler 及其,就好像您从未在那里一样。Typically if you are doing a background thread of that sort you want to use a Service to hold the thread. In your Activity you just bind to the Service, and when you get the Service instance you can pass it a Handler. The thread in the service can then access the Handler and send Messages through the Handler to the Activity. When the Activity finishes you
unbind()
, clear the Handler and its as if you were never there.好的,所以这个“日志记录”线程正在自行运行,将输入推送到某个磁盘文件或其他文件上。如果其他某个线程想要“挂钩”此数据,它必须向日志记录线程发出信号,以使用该数据调用其他内容并记录它。日志记录线程通常在某个输入队列上等待要记录的内容,是吗?如果是这样,请在其输入队列上向线程发送一条消息,告诉它使用要记录的所有数据调用方法 XXXX。日志记录线程可以保留要在每个日志记录操作上调用的所有事件的列表。
这有道理吗?基本上,这是对玛丽建议的扩展。记录器线程将获取消息并打开消息中的某些“Ecommand”枚举字段。伪 Pascal:
case inMessage.command :
ElogString:logThis(inMessage.logString);
EaddNotification:notifierList.add(inMessage.event);
EdeleteNotification:notifierList.delete(inMessage.event);
结尾;
“logThis”函数会将 logString 写入磁盘文件,然后使用数据调用 notifierList 中的每个事件。
这有道理吗?当我写这篇文章时确实如此,但今晚喝了艾伯特啤酒(5%)。
平均值,
马丁
OK, so this 'logging' thread is trundling along on its own, shoving input onto some disk file, or whatever. If some other thread wants to 'hook' this data, it has to signal the logging thread to call something else with the data as well as logging it. The logging thread is normally waiting on some input queue for stuff to log, yes? If so, send the thread a message on its input queue to tell it to call method XXXX with all the data to be logged. The logging thread can keep a list of all events to call on each logging action.
Does that make sense? Basically, it's an expansion on what Marie is suggesting. The logger thread would get messages and switch on some 'Ecommand' enumeration field in the message. Pseudo Pascal:
case inMessage.command of :
ElogString:logThis(inMessage.logString);
EaddNotification:notifierList.add(inMessage.event);
EdeleteNotification:notifierList.delete(inMessage.event);
end;
The 'logThis' function would write the logString to the disk file and then call every event in notifierList with the data.
Does this make sense? It did when I wrote it, but been on the Abbot ale tonight, (5%).
Rgds,
Martin
好吧,答案很简单,使用 Handler 并继续将此处理程序传递给您使用处理程序的方法,然后将成为主活动的连接器,并且如果需要也可以更改 GUI,如下所示:
声明此处理程序就像声明任何方法一样,不要将其嵌套在任何方法中
每次您需要将一些数据传递到主活动或 GUI 时,只需简单调用处理程序,例如:
等等...
希望这会有所帮助,
此致。
ok the answer is very simple, use Handler and keep passing this handler to the methods that you use the handler then will be your connector to the main activity, and will be able to change the GUI too if needed, as follow :
declare this handler the same way as you declare any method don't nest it in any method
every time that you need to pass some data to the main activity, or GUI, simple call to the handler for example :
and so on...
hope this helps,
Best regards.