AsyncTask has working:
doInBackground: Code performing long running operation goes in this method. When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.
onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
onPreExecute: This method is called before doInBackground method is called.
class Login extends AsyncTask {
private final static String TAG = "LoginActivity.Login";
@Override
protected void onPreExecute()
{
super.onPreExecute();
Log.d(TAG, "Executando onPreExecute Login");
//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 Login");
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();
}
}