2013-01-10 39 views

Trả lời

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! Đó là những gì tôi muốn ...: D – omerjerk

+0

Tôi rất vui được giúp đỡ :-) –

+0

Sự khác biệt TRUE giữa Mehul Ranpara và Mehul Joisar ở đây là Joisar muốn được giúp đỡ mà không nêu lên câu hỏi nhưng Ranpara muốn làm tức giận mọi người khác tại bài đăng này….Vì vậy, tôi chào Mehul Joisar vì đã tử tế với câu trả lời cho câu hỏi bất kể nhà nước ... – Vincy

6

Tại sao bạn không muốn chuyển bất kỳ đối số nào cho nó? Bạn nên giải thích ...

Đây là cách nó thường làm việc (ví dụ):

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

Và để thực hiện nó, bạn gọi:

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

Nguồn: Android docs

+0

Thông thường, các thông số được chuyển đến 'AsyncTask' nhưng không cần thiết – codeMagic

+0

không cần tôi chuyển đối số tới 'doInBakground'. phương pháp này chỉ mở tệp php trên máy chủ web của tôi theo phương thức POST. Và tôi chỉ cần nhận được phản hồi từ tệp php đó. Vì vậy, tôi không cần truyền bất kỳ đối số nào cho 'doInBackground'. – omerjerk

+0

Đơn giản - bạn chỉ cần sử dụng lớp 'Void'. Vì vậy, bạn sẽ có 'lớp học riêng DownloadFilesTask mở rộng AsyncTask {'. –

6

Tạo của bạn AsyncTask lớp như thể bạn không muốn chuyển bất kỳ thông số nào đến doInBackground:

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) {       

      } 
    } 

và bắt đầu AsyncTask như chưa trải qua bất kỳ thông số để thực hiện:

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

Tôi nhận thấy tính năng tự động hoàn tất của Android Studio không cung cấp cho bạn "", có thể giúp bạn tăng tốc. Ngoài ra, lưu ý rằng sự trở lại của 'doInBackground' thực sự phải là đối số thứ ba của lớp (" String "trong ví dụ được hiển thị). – Fattie

12

Theo AsyncTask, nó

AsyncTask<Params, Progress, Result> 
  • Params, loại các thông số gửi đến nhiệm vụ khi thực hiện.
  • Tiến trình, loại đơn vị tiến trình được xuất bản trong khi tính toán nền .
  • Kết quả, loại kết quả của tính toán nền.

Vì vậy, nếu bạn muốn bỏ trống trong doInBackground chỉ vượt qua khoảng trống thay cho Params.

Ví dụ mã:

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


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

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

Và gọi nó là:

new DownloadLink().execute(); 
+0

Điều đó có nghĩa là các tham số là mảng nhiệm vụ và mọi kiểu trả về của tác vụ là void – blackHawk

+0

@blackHawk - Có –

Các vấn đề liên quan