我的活动是登录页面。当用户单击时,asyncTask 会检查数据库中的凭据是否良好。在执行任务期间,我想显示一个 ProgressDialog 但在单击按钮后,它会保持按下状态 5 秒,然后我的 ProgressDialog quiclky 会出现(不到 1 秒)并出现吐司。
有我的 onClick 功能:
Button connect = (Button)findViewById(R.id.connectButton);
final EditText loginED = (EditText) findViewById(R.id.login);
final EditText passwordED = (EditText) findViewById(R.id.password);
connect.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
String login = loginED.getText().toString();
String password = passwordED.getText().toString();
String[] params = {login, password};
DoAsyncLogin doAsyncLogin = new DoAsyncLogin();
try {
String result = doAsyncLogin.execute(params).get();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
});
我的 AsyncTask :
private class DoAsyncLogin extends AsyncTask<String, Void, String>
{
ProgressDialog connectionProgressDialog = new ProgressDialog(MainActivity.this);
@Override
protected String doInBackground(String... params) {
return getLoginData(params);
}
protected void onPreExecute(){
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
connectionProgressDialog.setMessage("Logging in...");
connectionProgressDialog.show();
}
protected void onPostExecute(String result)
{
connectionProgressDialog.dismiss();
}
}
有任何想法吗 ?
谢谢 !