在Android中执行AsyncTask时如何使用Spinning或Wait图标?

时间:2014-04-24 11:16:32

标签: android android-intent android-fragments android-asynctask

我使用AsyncTask从我的Web服务中检索数据。我想在处理webservice时显示一些Spinning或Wait Icon屏蔽。我已经看到了一些有关这方面的解决方案,但它们写得非常冗长,我的要求不是要显示完成处理的剩余百分比,我只想在处理Web服务时显示一个图标,它应该在执行时解雇。我从我的活动中调用此代码,我想在我的活动上显示图标。请参阅下面的代码。请提出一些小而简单的解决方案。

public class AsyncLoginWarden extends AsyncTask<String, Integer, String> {


     protected String doInBackground(String...str) {

         WebserviceCall wb = new WebserviceCall();
         wb.param1 = str[0];
         wb.param2 = str[1];
         String response = wb.LoginWarden("LoginWarden");


         return response;
     }

     protected void onPostExecute(String result) {
          System.out.println("Successfully logged in."+result);  
     }

}

更新代码

package com.example.trafficviolationreporter;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;


public class AsyncLoginWarden extends AsyncTask<String, Integer, String> {
    ProgressDialog pd;
    Context co;
    MainActivity ma;
    String username, password;

    public AsyncLoginWarden(MainActivity ma, String username, String password) {
        this.ma = ma;
        this.co = ma;
        this.password = password;
        this.username = username;
        pd = new ProgressDialog(co);
    }

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

    protected String doInBackground(String... str) {

        WebserviceCall wb = new WebserviceCall();
        wb.param1 = str[0];
        wb.param2 = str[1];
        String response = wb.LoginWarden("LoginWarden");

        return response;
    }

    protected void onPostExecute(String result) {
        System.out.println("Successfully logged in." + result);
        pd.dismiss();
    }

}

2 个答案:

答案 0 :(得分:1)

您可以在异步类的preexcecute中创建进度对话框,并在异步类的onpostexecute中解除。以下是您将如何做到这一点:

public class AsyncLoginWarden extends AsyncTask<String, Integer, String> {
        ProgressDialog pd;
        Context co;
        YourActivity ma;
        String username, password;

        public AsyncLoginWarden(YourActivity ma, String username, String password) {
            this.ma = ma;
            this.co = ma;
            this.password = password;
            this.username = username;
            pd = new ProgressDialog(co);
            pd.setTitle("title");
            pd.setMessage("message");
        }

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

        protected String doInBackground(String... str) {

            WebserviceCall wb = new WebserviceCall();
            wb.param1 = str[0];
            wb.param2 = str[1];
            String response = wb.LoginWarden("LoginWarden");

            return response;
        }

        protected void onPostExecute(String result) {
            System.out.println("Successfully logged in." + result);
            pd.dismiss();
        }

    }

从活动中调用您的异步类:

YourActivity ma = this;
new AsyncLoginWarden(ma,username,password).execute();

答案 1 :(得分:0)

ProgressDialog pDialog;
public class AsyncLoginWarden extends AsyncTask<String, Integer, String> {

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

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(true);
        pDialog.show();

        }
     protected String doInBackground(String...str) {

         WebserviceCall wb = new WebserviceCall();
         wb.param1 = str[0];
         wb.param2 = str[1];
         String response = wb.LoginWarden("LoginWarden");


         return response;
     }

     protected void onPostExecute(String result) {
          System.out.println("Successfully logged in."+result);  

          if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }
     }

}

以上代码将在后台任务期间显示ProgressDialog,并在后台任务完成时将其解除。