[英]Thread in one class on completed can it let another class know about it?
In android have an activity (Main Activity) that calls another class (not an activity) to do something. 在android中有一个活动(Main Activity),该活动调用另一个类(不是活动)来做某事。 This class starts a thread to download data from the server.
此类启动线程以从服务器下载数据。 When the download is completed can I send a message to tell the main Activity class to do something else?
下载完成后,我可以发送一条消息告诉主Activity类做其他事情吗? I was looking at handlers but then the Handler needs to be defined in the same class.
我正在查看处理程序,但随后需要在同一类中定义处理程序。 Is there something else that I am missing?
还有其他我想念的东西吗?
EDIT: Thanks.. But my problem is the thread is started from the other class. 编辑:谢谢..但是我的问题是线程是从另一个类启动的。 The scenario is I have activity that is binded to a service and is receiving messages.
场景是我有绑定到服务并正在接收消息的活动。 I send the message to a processor class that processes the message.
我将消息发送到处理该消息的处理器类。 I return with a response and send in a new request.
我返回一个响应并发送新请求。 Now for one of the message I need to get data from the server(in another class) and on downloading it I need to send the response.
现在,对于其中一条消息,我需要从服务器(在另一个类中)获取数据,并在下载消息时发送响应。
Yes, you can use a Handler
created in your UI thread and post messages to it from your worker thread. 是的,您可以使用在您的UI线程中创建的
Handler
程序,并通过工作线程将消息发布到该处理程序。
A more general solution would be to use an event bus such as Otto or GreenRobot that can handle all the messaging in your app. 更通用的解决方案是使用事件总线,例如Otto或GreenRobot,它们可以处理应用程序中的所有消息传递。 I wrote a blog post about this in some detail.
我写了一篇关于此的博客文章 。
I recommend you to use AsyncTask
because that's its purpose, instead of trying to do it yourself. 我建议您使用
AsyncTask
因为这是它的目的,而不是自己尝试做。
If AsyncTask
is not what you're looking for (eg downloading takes too much time, you want to keep the thread running on the background, etc...), and all what you want is to do is GUI, you can just use runOnUIThread
on the thread. 如果不是您想要的
AsyncTask
(例如,下载花费了太多时间,您想让线程在后台运行,等等...),而您要做的就是GUI,您可以使用runOnUIThread
上的runOnUIThread
。
If previous solutions are a no-go, you can do as follows: 如果以前的解决方案不可行,则可以执行以下操作:
public class MainActivity extends Activity {
private void startDownload() {
// Start your thread passing this activity reference for example:
YourThreadStartClass.startThreadedJob(this);
}
public void onDownloadFinish() {
// Do your stuff on MainActivity after the download has finished
}
}
public class YourThreadStartClass {
public static void startThreadedJob(final MainActivity callback) {
new Thread(new YourRunnable(callback)).start();
}
}
// This is the Runnable your thread will be running
public class YourRunnable implements Runnable {
private final MainActivity callback;
public YourRunnable(final MainActivity activity) {
this.callback = activity;
}
@Override
public void run() {
// Do your stuff on this thread and when finished:
this.callback.onDownloadFinish();
}
}
EDIT: I've simplified a bit, but the actual right way in Java to do this would be to define an interface like 编辑:我已经简化了一点,但是Java中执行此操作的实际正确方法是定义一个像
public interface DownloadFinishedCallback {
void onDownloadFinish();
}
and make your MainActivity
(or any other class for that matter) implement this interface, and use a DownloadFinishedCallback
reference instead of a MainActivity
one in YourRunnable
. 并使您的
MainActivity
(或与此有关的任何其他类)实现此接口,并在YourRunnable
使用DownloadFinishedCallback
引用而不是MainActivity
YourRunnable
。
Example with interface: 接口示例:
public interface DownloadFinishedCallback {
void onDownloadFinish();
}
public class MainActivity extends Activity implements DownloadFinishedCallback {
private void startDownload() {
// Start your thread passing this activity reference for example:
YourThreadStartClass.startThreadedJob(this);
}
@Override
public void onDownloadFinish() {
// Do your stuff on MainActivity after the download has finished
}
}
public class YourThreadStartClass {
public static void startThreadedJob(final DownloadFinishedCallback callback) {
new Thread(new YourRunnable(callback));
}
}
// This is the Runnable your thread will be running
public class YourRunnable implements Runnable {
private final DownloadFinishedCallback callback;
public YourRunnable(final DownloadFinishedCallback callback) {
this.callback = callback;
}
@Override
public void run() {
// Do your stuff on this thread and when finished:
this.callback.onDownloadFinish();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.