【问题标题】:How to use AsyncTask to show a ProgressDialog while doing background work in Android? [duplicate]如何在 Android 中进行后台工作时使用 AsyncTask 显示 ProgressDialog? [复制]
【发布时间】:2011-04-23 01:25:00
【问题描述】:

可能重复:
Updating progress dialog in Activity from AsyncTask

我正在开发我的第一个 Android 应用程序,我需要在后台任务(在本例中只是服务器上的 http 调用)发生时显示 ProgressDialog。 我对此进行了一些研究,并且已经检查了与该主题相关的其他线程。

http://developer.android.com/reference/android/os/AsyncTask.html

Android show ProgressDialog until activity UI finished loading

Android SplashScreen

http://android-developers.blogspot.com/2009/05/painless-threading.html

等等。

比我写一点代码:

1) 在我的活动中,我声明一个变量为 ProgressDialog 类型

public class LoginActivity extends Activity {

    public static final String TAG = "LoginActivity";

    protected ProgressDialog progressDialog; 
...

2) 我还编写了一个内部类来根据需要扩展 AsyncTask,在 doInBackGround 中我调用了一个静态方法,该方法实际上对服务器执行 POST http 请求,在服务器端我阻止了服务器响应20 秒验证进度对话框。

class EfetuaLogin extends AsyncTask<Object, Void, String> {

        private final static String TAG = "LoginActivity.EfetuaLogin";

        @Override
        protected void onPreExecute()
        {
            Log.d(TAG, "Executando onPreExecute de EfetuaLogin");
        }


        @SuppressWarnings("unchecked")
        @Override
        protected String doInBackground(Object... parametros) {
            Log.d(TAG, "Executando doInBackground de EfetuaLogin");
            Object[] params = parametros;
            HttpClient httpClient = (HttpClient) params[0];
            List<NameValuePair> listaParametros = (List<NameValuePair>) params[1];
            String result = null;
            try{
            result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
            }catch (IOException e) {
                Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
                e.printStackTrace();
                return result;
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result)
        {
            progressDialog.dismiss();
        }

    } 

3)当按下按钮时,我会构建 ProgressDialog 并调用我创建的 AsyncTask:

    OnClickListener loginListener = new OnClickListener() {
      public void onClick(View v) {


//next line should start progress dialog in main thread ?????
   progressDialog = ProgressDialog.show(LoginActivity.this, "Login in", "Wait a moment please", true, false);

   //next couple of lines should do an ascyn call to server
   EfetuaLogin efetuaLogin = new EfetuaLogin();
   efetuaLogin.execute(params);
   try {
    //recover the server response and sets time out to be 25seconds
    sResposta = efetuaLogin.get(25, TimeUnit.SECONDS);

好吧,就是这样,我相信这是假设显示一个进度对话框,而 AsyncTask 将在后台查询服务器,但我得到的是在服务器响应到达之前没有进度条,而不是一小部分时间(更少超过 1 秒)进度显示并调用下一个 Activity。

正如我所提到的,我已经重新检查了这段代码,但根本找不到我出错的地方。 有什么建议吗?

提前谢谢你。

嗨,正如 Charlie Sheen(???) 在该线程的第一个答案中所建议的那样,我尝试更改了一些代码,现在它就像(不幸的是,到目前为止它没有按预期工作):

OnClickListener loginListener = new OnClickListener() {
        public void onClick(View v) {
                //async call????????
        new EfetuaLogin().execute(params);
...

然后在 AsyncTask 中完成所有处理响应的工作:

class EfetuaLogin extends AsyncTask<Object, Void, String> {

        private final static String TAG = "LoginActivity.EfetuaLogin";

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            Log.d(TAG, "Executando onPreExecute de EfetuaLogin");
            //inicia diálogo de progresso, mostranto processamento com servidor.
            progressDialog = ProgressDialog.show(LoginActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
        }


        @SuppressWarnings("unchecked")
        @Override
        protected String doInBackground(Object... parametros) {
            Log.d(TAG, "Executando doInBackground de EfetuaLogin");
            Object[] params = parametros;
            HttpClient httpClient = (HttpClient) params[0];
            List<NameValuePair> listaParametros = (List<NameValuePair>) params[1];
            String result = null;
            try{
            result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
            }catch (IOException e) {
                Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
                e.printStackTrace();
                return result;
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);

            if (result == null || result.equals("")) {
                progressDialog.dismiss();
                Alerta
                        .popupAlertaComBotaoOK(
                                "Dados incorretos",
                                "Os dados informados não foram encontrados no Sistema! Informe novamente ou cadastre-se antes pela internet.",
                                LoginActivity.this);
                return;
            }

            Log.d(TAG, "Login passou persistindo info de login local no device");
            ContentValues contentValues = new ContentValues();
            contentValues.put(AnototudoMetadata.LOGIN_EMAIL, sLogin);
            contentValues.put(AnototudoMetadata.LOGIN_SENHA, sSenha);
            contentValues.put(AnototudoMetadata.LOGIN_SENHA_GERADA, result);
            LoginDB loginDB = new LoginDB();
            loginDB.addLogin(LoginActivity.this, contentValues);
            Log.d(TAG, "Persistiu info de login no device, redirecionando para menu principal do Anototudo");
            Log.d(TAG, "O retorno da chamada foi ==>> " + result);
            // tudo ok chama menu principal
            Log.d(TAG, "Device foi corretametne autenticado, chamando tela do menu principal do Anototudo.");

            String actionName = "br.com.anototudo.intent.action.MainMenuView";
            Intent intent = new Intent(actionName);
            LoginActivity.this.startActivity(intent);
            progressDialog.dismiss();
        }

    } 

完成 OnClickListener:

OnClickListener loginListener = new OnClickListener() {
    public void onClick(View v) {
        Log.d(TAG, "Usuario logado, chamando menu principal");
        TextView tLogin = (TextView) findViewById(R.id.loginText);
        TextView tSenha = (TextView) findViewById(R.id.senhaText);
        String sLogin = tLogin.getText().toString();
        String sSenha = tSenha.getText().toString();

        if (sLogin.equals("") | sSenha.equals("")) {
            Alerta.popupAlertaComBotaoOK("Campos Obrigatórios",
                    "Os campos Login e Senha são obrigatórios para autenticação do Anototudo.", LoginActivity.this);
            return;
        } else {
            Pattern regEx = Pattern.compile(".+@.+\\.[a-z]+");
            Matcher matcher = regEx.matcher(sLogin);
            if (!matcher.matches()) {
                Alerta.popupAlertaComBotaoOK("Formato e-mail inválido", "O formato do campo e-mail está inválido",
                        LoginActivity.this);
                return;
            }
        }


        List<NameValuePair> listaParametros = new ArrayList<NameValuePair>();
        listaParametros.add(new BasicNameValuePair("login", sLogin));
        listaParametros.add(new BasicNameValuePair("senha", sSenha));

        Log.d(TAG, "valores recuperados dos campos de login e senha: " + sLogin + " | " + sSenha);

        // Reutiliza cliente HttpClient disponibilizado pela Aplicação.
        AnototudoApp atapp = (AnototudoApp) LoginActivity.this.getApplication();
        HttpClient httpClient = atapp.getHttpClient();

        //prepara lista de parametros para fazer chamada asíncrona no servidor para autenticar.
        Object[] params = new Object[2];
        params[0] = httpClient;
        params[1] = listaParametros;

        //faz chamada assincrona
        new EfetuaLogin().execute(params);
    }
};

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    将您的ProgressDialog 放入onPreExecute,示例代码如下:

    private ProgressDialog pdia;
    
    @Override
    protected void onPreExecute(){ 
       super.onPreExecute();
            pdia = new ProgressDialog(yourContext);
            pdia.setMessage("Loading...");
            pdia.show();    
    }
    
    @Override
    protected void onPostExecute(String result){
       super.onPostExecute(result);
            pdia.dismiss();
    }
    

    在您的onClickListener 中,只需将这一行放在里面:

    new EfetuaLogin().execute(null, null , null);
    

    【讨论】:

    • 我已经尝试过了(将 pd 放在 onPreExecute 中)也没有按预期工作。正如你提到的,我还需要传递一些参数来执行任务,所以我不能使用 execute(null)。我可以吗?请注意,我需要传递一些参数并从 AsyncTask 恢复响应,这就是我调用 AsyncTask.get 方法的原因。
    • @Marcos Maia:您创建了一个 AsyncTask 的对象并在其中存储了一个引用。在不创建引用的情况下执行它。 AsyncTask 用于短操作,因此请检查onPostExecute 中的结果,而不是尝试通过您的参考获得响应。当您单击Button 时,无论如何您都无法获得服务器响应。
    • @Marcos Maia:将所有必要的东西放在doInBackground 中。来自doInBackground 的传出结果传递给onPostExecute 参数,在那里你可以检查你的服务器响应——所有这些都在AsyncTask 中。关于您传递给AsyncTask 的值,我无法告诉您任何事情,因为缺少一些代码 sn-ps。
    • @Charlie Sheen:我已经尝试过了,仍然有相同的行为。我将在此主题中发布另一个答案,显示代码的详细信息,因为 cmets 代码未格式化。感谢您的帮助,但仍然有一些缺失的部分,因为它到目前为止还没有工作。 :(
    • @Marcos Maia:编辑你的问题。
    【解决方案2】:

    最终的解决方案是将所有代码从OnClickListener 转移到doInBackground 方法中的AsyncTask 实现。现在的代码是这样的:

    OnClickListener:

    OnClickListener loginListener = new OnClickListener() {
            public void onClick(View v) {
                /* Translation note: Original text: "Executando OnClickListener" */
                Log.d(TAG, "OnClickListener has been called");
    
                /* Translation note: Original text: "faz chamada assincrona" */
                // Make an asynchronous call
                new EfetuaLogin().execute();
            }
        }; 
    

    所有工作都发生在EfetuaLogin AsyncTask 实现中:

    class EfetuaLogin extends AsyncTask<Object, Void, String> {
    
            private final static String TAG = "LoginActivity.EfetuaLogin";
    
            protected ProgressDialog progressDialog;
    
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
                Log.d(TAG, "Executando onPreExecute de EfetuaLogin");
                //inicia diálogo de progresso, mostranto processamento com servidor.
                progressDialog = ProgressDialog.show(LoginActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
            }
    
    
            @SuppressWarnings("unchecked")
            @Override
            /* Translation note: Original text: "Object... parametros"
            protected String doInBackground(Object... parameters) {
                /* Translation note: Original text: "Executando doInBackground de EfetuaLogin" */
                Log.d(TAG, "Executing doInBackground of EfetuaLogin");
                TextView tLogin = (TextView) findViewById(R.id.loginText);
                TextView tSenha = (TextView) findViewById(R.id.senhaText);
                String sLogin = tLogin.getText().toString();
                String sSenha = tSenha.getText().toString();
    
                if (sLogin.equals("") | sSenha.equals("")) {
                    /*
                      Translation notes:
                      1) "Campos Obrigatórios" -> "Required fields"
                      2) "Os campos Login e Senha são obrigatórios para autenticação do Anototudo." -> "Login and Password fields are required for Anototudo authentication."
                    Alerta.popupAlertaComBotaoOK("Required fields", "Login and Password fields are required for Anototudo authentication.", LoginActivity.this);
                    progressDialog.dismiss();
                    return null;
                } else {
                    Pattern regEx = Pattern.compile(".+@.+\\.[a-z]+");
                    Matcher matcher = regEx.matcher(sLogin);
                    if (!matcher.matches()) {
                        /*
                          Translation notes:
                          1) "Formato e-mail inválido" -> "Invalid email format"
                          2) "O formato do campo e-mail está inválido" -> "The email field has an invalid format"
                        */
                        Alerta.popupAlertaComBotaoOK("Invalid email format", "The email field has an invalid format",
                                LoginActivity.this);
                        progressDialog.dismiss();
                        return null;
                    }
                }
    
    
                List<NameValuePair> listaParametros = new ArrayList<NameValuePair>();
                listaParametros.add(new BasicNameValuePair("login", sLogin));
                listaParametros.add(new BasicNameValuePair("senha", sSenha));
                /* Translation note: Original text: "valores recuperados dos campos de login e senha: " */
                Log.d(TAG, "values retrieved from login and password fields:" + sLogin + " | " + sSenha);
    
                /* Translation note: Original text: "Reutiliza cliente HttpClient disponibilizado pela Aplicação." */
                // Reuses `HttpClient` made available by the Application.
                AnototudoApp atapp = (AnototudoApp) LoginActivity.this.getApplication();
                HttpClient httpClient = atapp.getHttpClient();
    
                String result = null;
                try {
                    result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
                } catch (IOException e) {
                    Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
                    e.printStackTrace();
                    return result;
                }
                return result;
            }
    
            @Override
            protected void onPostExecute(String result)
            {
                super.onPostExecute(result);
    
                if (result == null || result.equals("")) {
                    progressDialog.dismiss();
                    /*
                      Translation notes:
                      1) "Dados incorretos" -> "Incorrect data"
                      2) "Os dados informados não foram encontrados no Sistema! Informe novamente ou cadastre-se antes pela internet." -> "The reported data was not found in the System! Please report again or sign up on the internet first."
                    */
                    Alerta.popupAlertaComBotaoOK(
                                    "Incorrect data",
                                    "The reported data was not found in the System! Please report again or sign up on the internet first.",
                                    LoginActivity.this);
                    return;
                }
                /* Translation note: Original text: "Login passou persistindo info de login local no device" */
                Log.d(TAG, "Login passed persisting local login info on device");
                ContentValues contentValues = new ContentValues();
                contentValues.put(AnototudoMetadata.LOGIN_EMAIL, sLogin);
                contentValues.put(AnototudoMetadata.LOGIN_SENHA, sSenha);
                contentValues.put(AnototudoMetadata.LOGIN_SENHA_GERADA, result);
                LoginDB loginDB = new LoginDB();
                loginDB.addLogin(LoginActivity.this, contentValues);
                /* Translation note: Original text: "Persistiu info de login no device, redirecionando para menu principal do Anototudo" */
                Log.d(TAG, "Persisting login info on device, redirecting to Anototudo main menu");
                /* Translation note: Original text: "O retorno da chamada foi ==>> " */
                Log.d(TAG, "The callback was ==>>" + result);
                /* Translation note: Original text: "tudo ok chama menu principal" */
                // everything ok call main menu
                /* Translation note: Original text: "Device foi corretametne autenticado, chamando tela do menu principal do Anototudo." */
                Log.d(TAG, "Device has been correctly authenticated by calling the main menu screen of Annotate.");
    
                String actionName = "br.com.anototudo.intent.action.MainMenuView";
                Intent intent = new Intent(actionName);
                LoginActivity.this.startActivity(intent);
                progressDialog.dismiss();
            }
    
        }
    

    现在它按预期工作,但我不得不说我有点困惑,因为AsyncTask 文档说您可以使用 execute 将参数传递给您的任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2019-01-19
      • 2020-07-31
      相关资源
      最近更新 更多