简体   繁体   中英

how to show the progress Dialog in Android?

I need to show the progress Message to the user.But i can't able to show that.Here is my code.What's wrong in my code.Guide me to do.

public class MyProgressDemo extends Activity {
/** Called when the activity is first created. */

private Button clickBtn;
public ProgressDialog progressDialog;
Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    clickBtn = (Button) findViewById(R.id.Button01);
    clickBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            progressDialog = ProgressDialog.show(MyProgressDemo.this, "",
                    "Please Wait");
            processThread();

        }

    });

}

protected void processThread() {

    handler.post(new Runnable() {

        @Override
        public void run() {
            longTimeMethod();
            UI();
            progressDialog.dismiss();
        }
    });

}

private void longTimeMethod() {
    try {
        String strMethod = "MethodName";
        String strUrl = "url";
        String strResponse = WebserviceCall.Mobileaappstore(strUrl,
                strMethod);
        Log.d("RES", strResponse);
    } catch (Exception e) {
        Log.e("Exc", e.getMessage());
    }

}

private void UI() {
    TextView tv = new TextView(this);
    tv.setText("This is new UI");
    setContentView(tv);
}

}

The method Handler.post, spawn a thread within the UI thread, so your longTimeMethod(); will be run in the UI thread, blocking it so. You should do it like this:

protected void processThread() {
    Thread t = new Thread(){
       longTimeMethod();
       // Sends message to the handler so it updates the UI
       handler.sendMessage(Message.obtain(mHandler, THREAD_FINISHED));
    }
    // Spawn the new thread as a background thread
    t.start
}

Your handler should look like this in order to manage the message

private Handler mHandler = new Handler() {
    @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case THREAD_FINISHED:
                       UI();
                       progressDialog.dismiss();
                       break    
                }   
        }
};

You can use either this solution or AsynTask, it's up to you, both work. Choose the one that best adapts to you.

What you are trying to do perfectly fits the AsyncTask framework in Android. Try to implement it by subclassing the AsyncTask class, taking care of putting all UI related stuff in the onPreExecute/onPostExecute methods and you long time method in the doInBackground method.

If you need stuff from the activity, either pass it as a parameter in the constructor of the AsyncTask or have your AsyncTask as an inner class of the activity.

To add to MarvinLabs' post, you can show and dismiss the ProgressDialog like this.

private class SubmitCommentTask extends AsyncTask<String, Void, Void> {
    ProgressDialog dialog;
    protected Void doInBackground(String... params) {
        // Your long running code here
        return null;
    }

    protected void onPreExecute() {
        dialog = ProgressDialog.show(DetailsInfo.this, "Submitting Comment", "Please wait for the comment to be submitted.", true);
    }

    protected void onPostExecute(Void Result)
    {
        dialog.dismiss();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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