Hi, as suggested by Charlie Sheen(???) in the first answer for this thread I have tryied changing a bit of my code and now it is like(Unfortunatelly it is not working as expected so far):OnClickListener loginListener = new OnClickListener() { public void onClick(View v) { //async call???????? new EfetuaLogin().execute(params);...然后在 AsyncTask 中完成所有处理响应的工作:And than do all the work to deal with response in the 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:Complete 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); }};推荐答案将您的 ProgressDialog 放在 onPreExecute 中,示例代码如下:Place your ProgressDialog in onPreExecute, sample code below:private ProgressDialog pdia;@Overrideprotected void onPreExecute(){ super.onPreExecute(); pdia = new ProgressDialog(yourContext); pdia.setMessage("Loading..."); pdia.show();}@Overrideprotected void onPostExecute(String result){ super.onPostExecute(result); pdia.dismiss();}在你的 onClickListener 中,把这一行放在里面:and in your onClickListener, just put this line inside:new EfetuaLogin().execute(null, null , null); 这篇关于在 Android 中执行后台工作时如何使用 AsyncTask 显示 ProgressDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 18:23