调用AsynchTask.get当Android的UI线程块() [英] Android UI Thread Block when calling AsynchTask.get()

查看:142
本文介绍了调用AsynchTask.get当Android的UI线程块()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动是一个登录页面。当用户点击,在数据库中的AsyncTask检查,如果凭据都不错。在任务中,我要显示ProgressDialog但点击按钮后,它保持了pssed 5秒钟,然后我ProgressDialog quiclky显示出来(小于1秒),并出现在致祝酒辞。$ P $

My activity is a login page. When the user clicks, an asyncTask check in a database if credentials are good. During the task, I want to display a ProgressDialog but after clicking on the button, it stays pressed for 5 seconds and then my ProgressDialog quiclky shows up (less than 1 second) and the toast appears.

还有就是我的onClick功能:

There is my onClick function :

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:

And my 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();
}
}

任何想法?

谢谢!

推荐答案

的问题是,你正在等待(阻塞)的 AsynchTask 以完成其在主线程中执行(这使得它没用):
请参阅AsynchTask本细则get方法这里:<一href=\"http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29\"相对=nofollow> AsynchTask.get()

The problem is that you are waiting (blocking) for the AsynchTask to finish its execution on the main thread ( which makes it useless ): See the documentations for AsynchTask get method here :AsynchTask.get()

相反,你应该使用 onPostExcute 回电话,让您的结果。

Instead you should use onPostExcute call back to get your results.

code:

@Override
public void onClick(View arg0) {

   String login = loginED.getText().toString();
   String password = passwordED.getText().toString();
   String[] params = {login, password};

   DoAsyncLogin doAsyncLogin = new DoAsyncLogin();
   doAsyncLogin.execute(params);
}

和你asynchTask:

and in your asynchTask:

protected void onPostExecute(String result){
  connectionProgressDialog.dismiss();
  Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}  

这篇关于调用AsynchTask.get当Android的UI线程块()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆