简体   繁体   中英

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

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

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.

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

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 .

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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