0

我想显示一个进度对话框,而我有两个线程一个接一个地运行,但是我使用的数据结构通过线程填充,变为空。因此,我使用 thread.get() 方法来等待线程完成....不知道如何解决这个问题,这是我的一个异步线程的示例:

private void performDetailSearch(String reference) {

    String addplus = searchterm.replace(" ", "+");  
    RestClientDS restpSd = new RestClientDS();
    String url = PLACES_DETAILS_URL +"reference="+ reference + "&sensor=false&key=" + API_KEY;
    Log.d("url",url);
    String[] URL = {url};
    restpSd.execute(URL);

    try {
        restpSd.get();
    } 
    catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
4

1 回答 1

0

使用AsyncTask而不是 Thread 并在一个任务完成后调用另一个任务。

AsyncTask 可以这样调用new FetchData().execute();

private class FetchData extends AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

    /** progress dialog to show user that the backup is processing. */
    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage(getResources().getString(
                R.string.Loading_String));
        this.dialog.show();
    }

    protected Boolean doInBackground(final String... args) {
        try {

            //do your background work

            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            //call the another asynctask from here.
           // new FetchData2().execute();
        }
    }
}
于 2012-10-15T05:27:52.807 回答