0

Here is the code im trying to call inside 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() updates a list view. The content of the list view is updated using the tread _t. But when i call this method i dont see any of the "waiting" message coming up. Phone freezes as it used to be before (when i was not running content on a thread). Any suggestions ?

dinesh707
  • 12,106
  • 22
  • 84
  • 134

4 Answers4

1

Use AsyncTask

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();
        }
    }
Nirali
  • 13,571
  • 6
  • 40
  • 53
1

Below Snippet will help you.

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

            };
Vipul
  • 27,808
  • 7
  • 60
  • 75
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();
        }
    }

}
Tho
  • 23,158
  • 6
  • 60
  • 47
Santosh
  • 611
  • 2
  • 6
  • 24
0

create the dialog as following

pd = ProgressDialog.show(this, "Loading..", "Please Wait..", true,false);
new Thread(new Runnable() {                 
            @Override
            public void run() { 
                                               // your code here
                    }
        }).start();             
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41