简体   繁体   中英

How i can use circular spinning progress bar in my async task class

I want to use spinning progress bar in asynctask class. Below is my asynctask class code.

Before i use progress dialog which works properly.

public class Downloader extends AsyncTask<Void,Void,Object> {


    Context c;
    String urlAddress;
    RecyclerView rv;
public Downloader(Context c, String urlAddress, RecyclerView rv) {
    this.c = c;
    this.urlAddress = urlAddress;
    this.rv = rv;
    }


@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Object doInBackground(Void... voids) {
    return this.downloadData();
}

@Override
protected void onPostExecute(Object data) {
    super.onPostExecute(data);

    if(data.toString().startsWith("Error"))
    {
        Toast.makeText(c,data.toString(),Toast.LENGTH_SHORT).show();
    }else new RssParser(c, (InputStream) data, rv).execute();
}

private Object downloadData()
{
    Object connection=Connector.connect(urlAddress);
    if(connection.toString().startsWith("Error"))
    {
        return connection.toString();
    }

Please comment if you want any other information.

If you want any customization for showing the progress bar in asycTask you can refer following link. Android custom progress indicator while doing AsyncTask

For my projects somtimes I use this method:

I attached ProgressBar into my layout, but I did it invisible. And in code I write this steps:

private class ParseTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {

       ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(ProgressBar.VISIBLE);

            //My code
            }




@Override
protected void onPostExecute(String strJson) {

    //My code

    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(ProgressBar.INVISIBLE);



}

}

So, I "turn on" ProgressBar in doInBackground and "turn off" it in onPostExecute

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