I have this code snippet, the problem with this code is that when I click the 'done' button the ProgressDialog is not showing.Please help
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
done = (Button)findViewById(R.id.sendbut);
done.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showProgressDialog();
query_str = getString();
startUploading();
}
});
}
public void s开发者_JAVA技巧howProgressDialog(){
GlobalClass.printLine("Showing progress dialog");
progressDialog = new ProgressDialog(Send.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Sending...");
progressDialog.setCancelable(false);
progressDialog.show();
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressDialog.setProgress(mProgressStatus);
Toast.makeText(getApplicationContext(), "Herererererer",Toast.LENGTH_LONG).show();
if(mProgressStatus == 100){
Toast.makeText(getApplicationContext(), "Done",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
}).start();
}
Did you try to use AsyncTask
?
Here is the doc: http://developer.android.com/reference/android/os/AsyncTask.html
You may want to replace the thread with the AsyncTask.
You should use AsyncTask which is also known as Painless threading.
Now, to show progressbar while processing in background, here is an example:
// ASync Task Begin to perform
private class performBackgroundTask extends Async Task < Void, Void, Void >
{
private ProgressDialog Dialog = new ProgressDialog( this);
protected void onPreExecute()
{
// here you have place code which you want to show in UI thread like progressbar or dialog or any layout .
Dialog.setMessage(" please wait...");
Dialog.show();
}
protected Void doInBackground(Void... params)
{
// write here the code to download or to perform any background task.
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss(); // here we have to dismiss the progress dialog
// To display fetched data in textview, imageview, or any
}
}
@Hari..
I think you are calling another functions from inside startUploading(),that's why ProgressDialog is not shown. Try creating the progressBar in XML and write all functions inside the button click. If that is a recursive function change your logic to call this inside the button click itself.
The answer posted above by Paresh Mayani will not work since you can't use
new ProgressDialog(this)
in the AsyncTask. "this" needs to be a Context.
精彩评论