0

这是我试图在里面调用的代码onCreate()

        _t = new TheThread(this);
        pd = new ProgressDialog(this);
        pd.setMessage("Trip Detection ..");
        pd.show();
        _t.start();
        while(_t.isAlive()){
            //DO NOTHING..WIAITING TILL MAIN THREAD FISHIN
        }
        printToScreen();
        pd.dismiss();          

printToScreen() 更新列表视图。使用tread _t 更新列表视图的内容。但是当我调用这个方法时,我没有看到任何“等待”消息出现。电话像以前一样冻结(当我没有在线程上运行内容时)。有什么建议么 ?

4

4 回答 4

1

使用异步任务

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

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

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

    /** application context. */

    protected void onPreExecute() {
         this.dialog.setMessage("Please wait");
         this.dialog.show();
    }

    protected Boolean doInBackground(final String... args) {
        try {
            downloadFile(b.getString("URL"));
            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) {
            Toast.makeText(ShowDescription.this,
                    "File successfully downloaded", Toast.LENGTH_LONG)
                    .show();
            imgDownload.setVisibility(8);
        } else {
            Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG)
                    .show();
        }
    }
于 2012-06-07T11:10:53.837 回答
1

下面的代码片段将为您提供帮助。

progressDialog.show();

    new Thread()
      {
        public void run()
         {
             //do async operations here

             handler.sendEmptyMessage(0);
         } 
      }.start();

    Handler handler = new Handler()
            {

                @Override
                public void handleMessage(Message msg)
                {
                    progressDialog.dismiss();
                    super.handleMessage(msg);
                }

            };
于 2012-06-07T11:11:31.120 回答
0
class doback extends AsyncTask < URL, Integer, Long > {

    protected Long doInBackground(URL...arg0) {
        try {

        } catch (Exception e) {

        }
        return null;
    }

    protected void onProgressUpdate(Integer...progress) {

    }

    protected void onPostExecute(Long result) {
        try {
            dialog.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
            dialog.dismiss();
        }
    }

}
于 2012-06-07T11:03:37.480 回答
0

创建对话框如下

pd = ProgressDialog.show(this, "Loading..", "Please Wait..", true,false);
new Thread(new Runnable() {                 
            @Override
            public void run() { 
                                               // your code here
                    }
        }).start();             
于 2012-06-07T11:08:02.233 回答