开发者

progressbar in android with thread

开发者 https://www.devze.com 2023-03-28 04:02 出处:网络
I am build one application in which, I can get data from a server so simply I want to put progress bar on it but I can\'t give specific time duration. it is dismiss randomly while fe开发者_高级运维tch

I am build one application in which, I can get data from a server so simply I want to put progress bar on it but I can't give specific time duration. it is dismiss randomly while fe开发者_高级运维tch entire data from server. so can you tell me how can I do this ?

can you give one simple example with elaboration


 @Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_detail);

    pd = ProgressDialog.show(this, "Please Wait", "Loading ....", true,true);
    Thread thread = new Thread(this);  //Implment Runnable;
    thread.start();
    //      setData();
}

@Override
public void run() 
{
    //Do your all work except User Interface Updation
    han.sendEmptyMessage(0);

}
private Handler han=new Handler()
{

    @Override
    public void handleMessage(Message msg) 
    {
    dismiss dialog  
    }

};


You can use these http://developer.android.com/reference/android/widget/ProgressBar.html sample but it's better to integrate with async task Example of async task and since you don't know how long it will take just use the onPostExecute to close the progress. hope it helps


What you need is to combine AsynTask and indeterminate ProgressDialog together. Something like this:

private class DownloadTask extends AsyncTask<...> {

 private ProgressDialog progressBar;

 protected void onPreExecute() {
     progressBar = ...;
     progressBar.show();
 }

 protected Long doInBackground() {
     // blah blah
 }

 protected void onPostExecute() {
     progressBar.dismiss();
 }
}

Don't forget to handle if user wants to cancel the progress.


U can use AsyncTask

private class Myclass extends AsyncTask<Void, Integer, Void> {

 ProgressDialog pDialog;

@Override
 protected void onPreExecute() {
     pDialog= ProgressDialog.show(context, "", "loading...");
     pDialog.show();
super.onPreExecute();
 }

@Override
 protected Long doInBackground(Void... unused) {
     // your code here
return null;
 }

@Override
 protected void onPostExecute(Void unused) {
     pDialog.dismiss();
 }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消