简体   繁体   English

使片段等待线程完成

[英]Make Fragment to wait for Thread to finish

In an Android app that I am developing I have a Fragment that starts a Thread that gets an image from a specific URL and after that shows that image on a ImageView in the Fragment. 在我正在开发的Android应用程序中,我有一个Fragment,它启动一个Thread,该Thread从特定的URL获取图像,然后在Fragment中的ImageView上显示该图像。 The problem that I have is that my app does not wait for the thread to finish as I was expecting. 我遇到的问题是我的应用程序没有像我期望的那样等待线程完成。

In AdvertisementFragment.java : 在AdvertisementFragment.java中:

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // Obtain the advertisement controller.
        controller = new AdvertisementController();

        // Shows a spinner while fetching the advertisement image.
        progressDialog = ProgressDialog.show(getActivity(), "", getString(R.string.fetchingAdvImage));

        controller.showAvertisementImage2();

        // Destroy the spinner
        progressDialog.dismiss();

    return inflater.inflate(R.layout.fragment_advertisement, container, false);
}

The controller has this method: 控制器具有以下方法:

    public void showAvertisementImage2() {
    GetAdvertisementImageThread advertisementImageThread = new GetAdvertisementImageThread("Advertisement image thread", advertisementData);
    try {
        advertisementImageThread.t.join();
        Log.v("Thread", "Se pone en espera.");
    } catch (InterruptedException e) {
        Log.v("Thread", "Se llanza la exception.");
        e.printStackTrace();
    }
}

And in the GetAdvertisementImageThread class: 在GetAdvertisementImageThread类中:

GetAdvertisementImageThread(String threadname, AdvertisementData advertisementDataPassed) { name = threadname; GetAdvertisementImageThread(String threadname,AdvertisementData advertisingDataPassed){name = threadname; advertisementData = advertisementDataPassed; advertisingData = advertisementDataPassed;

advertisementData = new AdvertisementData();

t = new Thread(this, name);
Log.v("Thread", "New thread: " + t);
t.start();

} }

public void run() {
 try {

    // Connect to the url.
    in = openHttpConnection(url);

    // If the connection was no successful finish the execution.
    if (in == null) return;

    // Read the InputStream character by character and add it to the pageSourceCode String variable.
    InputStreamReader isr = new InputStreamReader(in);
    int charRead;
    pageSourceCode = "";
    char[] inputBuffer = new char[BUFFER_SIZE];
    .....
    }
}

I was expecting for the Fragment to wait for the Thread to finish but it does not and instead it destroys the 
ProgressDialog before the image is fetched and get the image afterwards. 
I thought that the .join will make it wait for the Thread to finish but it seems it does not. 
What am I doing wrong?

Waiting for the thread to finish a URL data fetching will be so lagy especially with low speed internet connection. 等待线程完成URL数据提取将非常麻烦,尤其是在低速Internet连接的情况下。 I suggest instead of waiting it to finish, let it take its time and display a message of fetching data; 我建议不要等待它完成,而要花一些时间并显示一条获取数据的消息。 to keep it user friendly. 使其易于使用。

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

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