1
我的活動是一個登錄頁面。當用戶點擊時,如果憑據很好,則在數據庫中執行asyncTask檢查。在任務期間,我想顯示ProgressDialog,但點擊按鈕後,它會保持按住5秒鐘,然後我的ProgressDialog quiclky顯示(小於1秒),然後出現Toast。調用AsynchTask.get時的Android UI線程塊()
還有就是我的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();
}
}
任何想法?
謝謝!