2012-03-21 64 views
0

我開發了我的第一個Android應用程序。我嘗試了每一個在本頁面和其他頁面中找到的代碼。那麼,我的問題是需要使用Internet服務登錄用戶,所以我使用AsyncTask類,但是當我嘗試將ProgressDialog添加到背景方法中時,此對話框稍後僅在後面顯示後臺方法完成。看起來UI在後臺進程運行時被阻塞。在AsyncTask類中使用ProgressDialog

這是我的活動和異步類的代碼。

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

}

謝謝您的幫助大家!

回答

2

根本點,使你的UI線程塊是:

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

通過調用AsyncTask.get(),你實際上是讓你的UI線程塊並等待工作線程(AKA。AsyncTask.doInBackground())完成。換句話說,通過這樣做,你的AsyncTask與UI線程同步運行。嘗試使用:

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

希望這有助於。

+0

非常感謝yorkw!它工作正常! – Vistiyos 2012-03-21 23:08:11

0

- > protected ProgressDialog progressDialog;

寫這條線的一流水平..

+0

是什麼意思?我應該寫沒有保護屬性? – Vistiyos 2012-03-21 16:54:36

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

}