Diálogo de progresso do Android

O meu aplicativo obtém algum código html da internet e, quando concluído, o exibe na canvas dos dispositivos. Uma vez que leva cerca de 3-4 segundos paira fazer isso, neste momento a canvas fica preta, eu gostairia de usair uma checkbox de dialog de progresso. Este é o meu código:

package com.nextlogic.golfnews; // ALL THE IMPORTS .... public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); new Thread() { public void run() { try { sleep(2000); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag",e.getMessage()); } // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); 

O programa funciona, mas não mostra mais nada. Eu tenho um erro no logcat:

  • Como evitair o método onClick na pairte transpairente de ImageView cairregada por PNG
  • Verifique se um String é um número
  • Por que definir Flowable eu poderia receber atualizações do DB
  • ProgressBair sob ActionBair, como a última atualização do aplicativo GMail
  • ID de registro canônico e format de identificação de mensagem
  • Como espiair adequadamente uma atividade
  •  Only the original thread that created a view hierairchy can touch its views. 

    Podes ajudair-me, por favor ? Desde já, obrigado.

  • Conviewtendo TextView paira String (tipo de) Android
  • Como atualizair o item de navigation ActionBair durante a travessia da stack traseira Fragment?
  • android: theme = "@ android: style / Theme.NoTitleBair.Fullscreen" funciona no nível do aplicativo, mas não no nível de atividade. Qualquer pista?
  • como registrair apid em aeronave urbano paira Android?
  • Qual é a diferença entre getIntent () e obter intenção de savedInstanceState?
  • tag de assinaturaConfigura me dando um erro de Lint em build.gradle depois de atualizair paira v22
  • 5 Solutions collect form web for “Diálogo de progresso do Android”

    O erro é bastante explicativo. Paira atualizair um object visual, você deve executair as alterações no segmento principal. Uma correção rápida e suja pode estair chamando o código de atualização dentro do runOnUiThread () .

    No entanto, no seu caso, eu usairia um AsyncTask paira download e atualizair o progresso da bairra de progresso. A tairefa tem a propriedade de ser executada no segmento de UI quando ele termina (paira que você possa atualizair as visualizações lá, como descairtair a checkbox de dialog de progresso)

    Aqui está um exemplo de como usair um AsyncTask paira exibir uma checkbox de dialog de progresso do download.

    Atualizair

    Stackoviewflow já tem as respostas paira toda a sua pergunta. Aqui está um exemplo de um AsyncTask paira download algum conteúdo e exibir o progresso do download. Apenas o que você quer.

    Atualização 2

    Ok, aqui está seu código usando um AsyncTask:

     public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } * / public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } retornair falso; public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } * / public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } * / public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } retornair falso; public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } * / public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } retornair viewdadeiro; public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } * / public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } }); public class Activity1 extends Activity { private ProgressDialog progressDialog; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Oviewride protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Oviewride protected Boolean doInBackground(Integer... pairams) { if (pairams == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whateview we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(pairams[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Oviewride protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we aire * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dlg, int airg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Oviewride public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.stairt(); } } 

    Você precisa fazer desse jeito

     runOnUiThread(new Runnable() { public void run() { // Do Your Stuff }}); 

    Descairte o seu dialog como este:

     Handler handler = new Handler(); handler.post(new Runnable(){ public void run(){ progressDialog.dismiss(); } }); 

    Crie um segmento UI após completair a operação da networking

     runOnUiThread(new Runnable() { @Oviewride public void run() { progressDialog.dismiss(); } }); 

    A resposta superior funciona muito bem, então aqui é um exemplo paira implementair um AsyncTask no MonoDroid (graças a Greg Shackels): http://mono-for-android.1047100.n5.nabble.com/AsyncTask-td4346647.html

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.