简体   繁体   English

如何实现Thread与AsyncTask之间的通信

[英]How to implement the communication between Thread and AsyncTask

I am developing an application using Android SDK. 我正在使用Android SDK开发应用程序。 In this application I am facing a problem when an Activity starts a Thread . 在此应用程序中,当Activity启动Thread时,我面临一个问题。 The new Thread starts an AsyncTask , but the Thread has to wait for completion of the AsyncTask execution. Thread启动AsyncTask ,但Thread必须等待AsyncTask执行完成。

Is it possible to start AsyncTask in a thread, and if so, how should it be implement? 是否可以在线AsyncTask ,如果可以,应如何实现? Please can anyone share your experience with me about this kind of tasks. 请任何人与我分享您对此类任务的经验。

Thanks in advance 提前致谢

  1. You should not create an AsyncTask from a thread other than main thread. 您不应从主线程以外的其他线程创建AsyncTask。
  2. If you should wait anyway, why create AsyncTask? 如果仍然要等待,为什么要创建AsyncTask? perform it on the thread. 在线程上执行它。

just say thread to sleep for some times and then check a variable again like this in below code someValue was setted in asynctask postExecute method 只是说线程要休眠一段时间,然后再在下面的代码中像这样检查一个变量,在asynctask postExecute方法中设置了someValue

new Thread(new Runnable(){
    @Override
    public void run(){
        while(!someValue) {
            try {
                Thread.sleep(200);
            } catch {
                Log.i("LOG", "ERROR");
            }
        }
        Log.i("LOG", "Async Task Finished");
    }
}).start();

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

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