简体   繁体   中英

android asyncTask dialog Circle

I been searching all day today trying for find some example code or tutorials how to create a progress circling while the task is being done. The time for this task to complete is vary accordingly and there are lots of samples out there that are using the Thread.sleep(xxxx) make it circling but this is not efficient. Here is what I want to do, I want to load a ListView that populated from a web service using JSON after a button is clicked. The listview is loading up perfectly fine, but it take about 5-10 seconds to load up depending on the size, so I want to show the spinning circling while the user is waiting. Can someone please share some sample code on how to achieve that?

Thank you

new Load().execute(); to call.

class Load extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressDialog progDailog = new ProgressDialog(Activity.this);
            progDailog.setMessage("Loading...");
            progDailog.setIndeterminate(false);
            progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDailog.setCancelable(true);
            progDailog.show();
        }
        @Override
        protected String doInBackground(String... aurl) {
            //do something while spinning circling show
            return null;
        }
        @Override
        protected void onPostExecute(String unused) {
            super.onPostExecute(unused);
            progDailog.dismiss();
        }
    }
private class LoadAssync extends AsyncTask<String, Void, Void> {



    protected void onPreExecute() {

        ProgressDialog dialog;
                   dialog.setMessage("Loading...");
    dialog.show();
    }

    protected Void doInBackground(final String... args) {
        // you can do the code here
        return null;


    }

    protected void onPostExecute(final Void unused) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

    }
}

u can call assync like this

 LoadAssync mAsyync=new LoadAssync();
 mAsyync.execute(null);

You can try following code,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.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