在AsyncTask类中使用ProgressDialog

时间:2012-03-21 15:54:17

标签: android android-asynctask progressdialog

我正在开发我的第一个Android应用程序。我尝试了在本页和其他人中找到的每一段代码。好吧,我的问题是需要使用Internet服务登录用户,所以我使用AsyncTask类,但是当我尝试将ProgressDialog添加到后台方法时,此对话框仅在一秒钟后显示后台方法已完成。在后台进程运行时,似乎UI被阻止了。

这是我的活动代码和异步类。

public class PanelAdministracion extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.paneladministracion);
    try {
        Bundle datos = this.getIntent().getExtras();
        Map<String,String> credenciales = new HashMap<String,String>();
        credenciales.put("usuario", datos.getString("usuario"));
        credenciales.put("password", datos.getString("password"));
        new ObtenerDatos().execute(credenciales,null,null).get();
        MyPagerAdapter adapter = new MyPagerAdapter(this);
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

private class ObtenerDatos extends AsyncTask< Map<String,String>, Void, Void>{

    protected ProgressDialog progressDialog;
    private final static String TAG = "LoginActivity.EfetuaLogin";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.v(TAG, "Executando onPreExecute de EfetuaLogin");
        //inicia diálogo de progresso, mostranto processamento com servidor.
        progressDialog = ProgressDialog.show(PanelAdministracion.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
    }

    @Override
    protected Void doInBackground(Map<String,String>... params) {
        Log.d(TAG, "Executando doInBackground de EfetuaLogin");
        try {
            if(Usuario.login(params[0].get("usuario"), params[0].get("password"))){
                Usuario.obtenerNotificaciones();
                Usuario.obtenerPeliculas();
                Usuario.obtenerSeries();
            }else{
                Intent volver = new Intent(PanelAdministracion.this,SerieslyActivity.class);
                PanelAdministracion.this.startActivity(volver);
            }
        } catch (NotSignInException e) {
             e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (DOMException e) {
            e.printStackTrace();
        } catch (GetDataSerieException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
    }
}

}

谢谢大家的帮助!

3 个答案:

答案 0 :(得分:2)

使您的UI线程阻止的根点是:

new ObtenerDatos().execute(credenciales,null,null).get();

通过调用AsyncTask.get(),您实际上正在使用UI线程块并等待工作线程(AKA.AsyncTask.doInBackground())完成。换句话说,通过这样做,您的AsyncTask与UI线程同步运行。尝试使用:

new ObtenerDatos().execute(credenciales,null,null);

希望这有帮助。

答案 1 :(得分:0)

- &GT; protected ProgressDialog progressDialog;

在班级写下这一行..

答案 2 :(得分:0)

我会推荐这样的东西......

public class PanelAdministracion extends Activity {

private ProgressDialog mProgressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.paneladministracion);
    try {
        Bundle datos = this.getIntent().getExtras();
        Map<String,String> credenciales = new HashMap<String,String>();
        credenciales.put("usuario", datos.getString("usuario"));
        credenciales.put("password", datos.getString("password"));

        // Keep progressDialog outside of AsyncTask...
        // This all could be put in a separate method to clean things up...
        mProgressDialog = ProgressDialog.show(PanelAdministracion.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);

        new AsyncTask<Map<String,String>, Void, Void>()
        {
            @Override
            protected Void doInBackground(Map<String,String>... params) {
                // Your other code goes here...
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                mProgressDialog.dismiss();
            }

        }.execute(credenciales);


        MyPagerAdapter adapter = new MyPagerAdapter(this);
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);

        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

}