简体   繁体   English

将数据从不同的类发送到主UI-Thread

[英]Send data from a different Class to the main UI-Thread

could somebody tell me how I can send data from a different Class (not an High-level class eg not an Activity, Service ... etc) to the UI-Thread? 有人可以告诉我如何从不同的类(不是高级类,例如不是活动,服务等)向UI-Thread发送数据吗? To be more concrete in my App I have a class which connects to a server returning a JSONObject from a Thread. 为了在我的应用程序中更具体,我有一个类连接到从Thread返回JSONObject的服务器。 From that point I want to send back the JSON to the UI-Thread (My Activity). 从那时起,我想将JSON发送回UI-Thread(我的活动)。 I've read something about Handler and Looper but this is very confusing for me specially because the most examples are based on the fact that the data transfer occurs in 2 Threads in the same Class. 我已经阅读了一些关于Handler和Looper的内容,但这对我来说非常困惑,因为大多数示例都是基于数据传输发生在同一类中的2个线程中的事实。 I would like to have the steps with a code snippet to know where and what I should implement. 我想通过代码片段来了解我应该实现的位置和内容。 Thank you for helping 感谢您的帮助

There is a specific Thread-implementation in Android for just that. Android中有一个特定的Thread实现。 The AsyncTask lets you do something on the UI-Thread before and after the execution in a background thread. AsyncTask允许您在后台线程中执行之前和之后在UI-Thread上执行某些操作。 It is mainly used for calculating something in the background and then updating the UI or to display some kind of dialog while the background thread works. 它主要用于在后台计算某些内容,然后更新UI或在后台线程工作时显示某种对话框。

I find it fairly simple to use. 我发现它使用起来相当简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM