2016-04-26 9 views
1

In einem Asyntask, ich einen Fortschritt Dialog in globalen Variablen von Asyntask instanziiert:ProgressDialog erscheint nicht im Verfahren onProgressUpdate

ProgressDialog progressDialog; 

Dann in DoInBackGround, ich nenne "publishProgress", so onProgressUpdate gerufen. Es , in onProgressUpdate, ich habe den folgenden Code:

this.progressDialog=new ProgressDialog(mActivity); 
       this.progressDialog.setCancelable(true); 
       this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
       this.progressDialog.setTitle("Iniciando la comunicación"); 
       this.progressDialog.show(); 

Ich weiß nicht, warum, die progressDialog wird nie angezeigt. Ich habe versucht, Toast mit dem gleichen Kontext zu verwenden, und es funktioniert gut. Obwohl progressDialog nicht.

--------- UPDATE --------------

Schließlich habe ich es herausfinden Benachrichtigungen verwenden.

+0

habe ich bereits t ried es, ich arbeite nicht –

+0

hast du meine antwort überprüft? –

+0

@ PhanVănLinh Ich habe deine Antwort implementiert, aber es hat nicht geklappt. –

Antwort

3

Ihren AsyncTask wie diese

private class YourTask extends AsyncTask<String, String, String> { 
    ProgressDialog progressDialog; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     ... 
     // show the dialog 
     this.progressDialog=new ProgressDialog(mActivity); 
      this.progressDialog.setCancelable(true); 
      this.progressDialog.setProgressStyle(ProgressDialog. STYLE_HORIZONTAL); 
      //this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      this.progressDialog.setTitle("Iniciando la comunicación"); 
      this.progressDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... urls) { 

     ... 
     publishProgress("" + progress); 
     ... 
    } 

    protected void onProgressUpdate(String... progress) {   
     progressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // dismiss the dialog   
     progressDialog.dismiss(); 
     ... 
    } 
} 

auch Sie den Fortschritt der öffentlichen wollen, wenn die AsyncTask läuft, so Sie benötigen ein STYLE_HORIZONTALProgressBar nicht STYLE_SPINNERProgressBar

Hope diese Hilfe

0

Ich sah dieses Problem vor zwei Jahren. Es gibt etwas unvorhersehbares Verhalten während der Interaktion mit dem Haupt-UI-Thread. Die Lösung, die ich fand, ist Thread statt AsyncTack zu verwenden.

Ein schnelles einfaches Beispiel:

new Thread(new Runnable() { 
        public void run() { 

//background code to calculate 
//for example 
for(int i = 0; i < 100000000L; i++){ 
myHugeCalc(i); 
final int prog = i; 
runOnUiThread(new Runnable() { 
        public void run() { 

         pbtext.setText("My progress: " + prog); 

        } 
       }); 
} 

}).start(); 
0

Es gibt einen anderen Weg, dies zu tun ... , wenn Sie diesen Sync-Prozess aus Ihrer Klasse aufrufen werden, starten Sie den Fortschritt dort Dialog zeigt, und onPostExecute es abzubrechen. Es wird tun, um Ihre Arbeit ... aber die beste Praxis ist, es zu tun in onPreExecute