2013-01-10 22 views

cevap

78
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 

public class AsyncExample extends Activity{ 


private String url="http://www.google.co.in"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 


@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 

    new AsyncCaller().execute(); 

} 

private class AsyncCaller extends AsyncTask<Void, Void, Void> 
{ 
    ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this); 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     //this method will be running on UI thread 
     pdLoading.setMessage("\tLoading..."); 
     pdLoading.show(); 
    } 
    @Override 
    protected Void doInBackground(Void... params) { 

     //this method will be running on background thread so don't update UI frome here 
     //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here 


     return null; 
    } 

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

     //this method will be running on UI thread 

     pdLoading.dismiss(); 
    } 

    } 
} 
+0

Thanx! İstediğim şeye benziyor ...: D – omerjerk

+0

Yardım ettiğim için mutluyum :-) –

+0

Mehul Ranpara ve Mehul Joisar arasındaki TRUE farkı burada Joisar soru sormadan yardım istiyor ama Ranpara herkesi kızdırmak isteyen herkesi kızdırmak istiyor bu gönderide…Bu nedenle, Mehul Joisar'ı, devletten bağımsız bir soruya yanıt olarak selamlıyorum… – Vincy

6

Neden herhangi bir argümana geçmek istemiyorsunuz?

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 

Ve sizin çağrıyı yürütmek için:

new DownloadFilesTask().execute(url1, url2, url3); 

Kaynak: Sen

Bu genellikle (örnek) işliyor ... açıklamalıdır oluşturun Android docs

+0

Genellikle, 'AsyncTask' parametrelerine geçirilir ama gerekli değildir – codeMagic

+0

'doInBakground' için bir argümana geçmeye gerek yok. Bu yöntem sadece web sunucumda POST yöntemi ile bir php dosyası açar. Ve sadece bu php dosyasından bir cevap almam gerekiyor. Bu yüzden, herhangi bir argümanı "doInBackground" alanına aktarmam gerekmiyor. – omerjerk

+0

Basit - sadece "Void" sınıfını kullanıyorsunuz. Yani, özel sınıf DownloadFilesTask, AsyncTask {'. –

6

senin doInBackground:

için herhangi bir parametre iletmek istemiyorsanız AsyncTask sınıfı
public class LongOperation extends AsyncTask<Void, Void, String> { 


      public LongOperation(Context context) { 

      } 

      @Override 
      protected void onPreExecute() { 

      } 

      @Override 
      protected String doInBackground(Void... params) { 

       return null; 
      }  

      @Override 
      protected void onPostExecute(String result) {       

      } 
    } 

ve AsyncTask başlar başlamaz yürütmek için herhangi bir parametre geçmeden: AsyncTask göre

LongOperation longOperation = new LongOperation(this); 
    longOperation.execute(); 
+0

Android Studio otomatik tamamlama özelliğinin size "" değerini vermediğini fark ettim. Ayrıca, 'doInBackground' için dönüşün gerçekten de sınıfın üçüncü bağımsız değişkeni olması gerektiğini unutmayın (gösterilen örnekte" String "). – Fattie

12

, onun

AsyncTask<Params, Progress, Result> 
  • Parametreler, gönderilen parametrelerin tipi yürütme görevi.
  • İlerleme, arka plan hesaplama sırasında yayınlanan ilerleme birimlerinin türü.
  • Sonuç, arka plan hesaplama sonucunun türü. Eğer doInBackground boşluğu geçmek istiyorsan

Dolayısıyla, sadece PARAMS yerine boşluğu geçerler.

örnek kod:

class DownloadLink extends AsyncTask<Void, Void, Void> { 


     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 

      //Do Your stuff here.. 
      return null; 
     } 
    } 

Ve diyoruz:

new DownloadLink().execute(); 
+0

Bu, paramik görev dizisi ve tüm görevlerin dönüş türünün geçersiz olduğu anlamına gelir – blackHawk

+0

@blackHawk - Evet –

İlgili konular