2014-07-16 18 views
7

Tôi muốn biết sự khác biệt giữa bộ nạp Android và AsyncTask, đây bản demo trên Loader:Sự khác biệt giữa trình tải hoặc AsyncTask là gì?

package com.android.loaderdemo; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.LoaderManager.LoaderCallbacks; 
import android.content.AsyncTaskLoader; 
import android.content.Context; 
import android.content.Loader; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.TextView; 

import com.android.webprocessor.Http_GetServices; 

public class MainActivity extends Activity implements LoaderCallbacks<String> { 
    TextView txt_username, txt_userEmail, txt_password, txt_role, txt_secretQuestion, txt_answer, txt_zipcode; 

    private static String url = "http://dotstg1.xyz.com/loud/webservcies/GetUserProfile.svc/GetUserProfile/124"; 
    static String response; 
    static String name, email, Pasword, Answer, RoleId, SecretQuestion, Zip; 

    static String useResult = null; 

    static JSONArray userParams = null; 

    private static final int THE_LOADER = 0x01; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.e("onCreate", "onCreate"); 
     // getSuLoaderManager().initLoader(THE_LOADER, null, this).forceLoad(); 
     getLoaderManager().initLoader(THE_LOADER, null, this).forceLoad(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     Log.e("onCreateOptionsMenu", "onCreateOptionsMenu"); 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public Loader<String> onCreateLoader(int arg0, Bundle arg1) { 
     Log.e("onCreateLoader", "onCreateLoader"); 
     SampleLoader loader = new SampleLoader(this); 

     return loader; 
    } 

    @Override 
    public void onLoadFinished(Loader<String> arg0, String arg1) { 
     txt_username = (TextView) findViewById(R.id.name); 
     txt_userEmail = (TextView) findViewById(R.id.email); 
     txt_password = (TextView) findViewById(R.id.password); 
     txt_role = (TextView) findViewById(R.id.role); 
     txt_secretQuestion = (TextView) findViewById(R.id.secretquestion); 
     txt_zipcode = (TextView) findViewById(R.id.zipcode); 
     txt_answer = (TextView) findViewById(R.id.answer); 

     txt_username.setText(name); 
     txt_userEmail.setText(email); 
     txt_password.setText(Pasword); 
     txt_role.setText(RoleId); 
     txt_secretQuestion.setText(SecretQuestion); 
     txt_answer.setText(Answer); 
     txt_zipcode.setText(Zip); 
     Log.e("onLoadFinished", "onLoadFinished"); 
    } 

    @Override 
    public void onLoaderReset(Loader<String> arg0) { 
     Log.e("onLoaderReset", "onLoaderReset"); 

    } 

    private static class SampleLoader extends AsyncTaskLoader<String> { 

     @Override 
     public Context getContext() { 
      Log.e("getContext", "getContext"); 
      return super.getContext(); 
     } 

     @Override 
     public int getId() { 
      Log.e("getId", "getId"); 
      return super.getId(); 
     } 

     public SampleLoader(Context context) { 

      super(context); 
      Log.e("SampleLoader", "SampleLoader"); 
     } 

     @Override 
     public String loadInBackground() { 
      Log.e("loadInBackground", "loadInBackground"); 
      try { 
       response = Http_GetServices.connect(url); 
       JSONObject jsonObject = new JSONObject(response); 
       JSONObject json2 = jsonObject.getJSONObject("GetUserPrfResult"); 
       String test = (String) json2.get("Descritption"); 
       JSONObject json3 = json2.getJSONObject("GetUserPrfParams"); 

       name = (String) json3.get("Name"); 
       email = (String) json3.get("Email"); 
       Pasword = (String) json3.get("Pasword"); 
       RoleId = String.valueOf(json3.getInt("RoleId")); 
       SecretQuestion = String.valueOf(json3.get("SecretQuestion")); 
       Answer = (String) json3.get("Answer"); 
       Zip = String.valueOf(json3.get("Zip")); 

      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

    } 

} 
+0

Hoàn toàn phụ thuộc vào: Yêu cầu của bạn, lược đồ bảng cơ sở dữ liệu và mối quan hệ, nếu bảng của bạn hỗ trợ khóa chính tăng tự động và chỉ có một bảng dữ liệu phải được hiển thị Trình tải là phù hợp. Trong khi nhiệm vụ không đồng bộ là tốt nhất để lưu/cập nhật/xóa các hoạt động vv, những người đang sống ngắn. –

+0

https://www.javacodegeeks.com/2013/01/android-loaders-versus-asynctask.html –

Trả lời

8

Từ here:

Một lớp con của Máy bốc hàng là AsyncTaskLoader. Lớp này thực hiện cùng chức năng với AsyncTask, nhưng tốt hơn một chút. Ứng dụng có thể xử lý các thay đổi cấu hình Hoạt động dễ dàng hơn và hoạt động trong vòng đời vòng đời của Phân đoạn và Hoạt động. Điều thú vị là AsyncTaskLoader có thể được sử dụng trong mọi tình huống mà AsyncTask là đang được sử dụng. Dữ liệu bất cứ lúc nào cần được tải vào bộ nhớ cho hoạt động /Phân đoạn để xử lý, AsyncTaskLoader có thể thực hiện công việc tốt hơn.

5

Theo tôi: Trình tải là tốt hơn. vì tôi đã sử dụng AsyncTask một năm trước đây, đó thực sự là một cơn ác mộng đối với tôi, bởi vì bạn không thể kiểm soát toàn bộ tiến trình ngay lập tức, như đôi khi, cũng có một asynctask chạy trên hoạt động, nhưng bạn muốn thoát hoạt động, bạn nên gọi asynctask.cancel(), nhưng, phương pháp này: cancel() không hủy đường thẳng asynctask.so trong trường hợp này, ứng dụng của bạn sẽ bị lỗi do điều này. vì vậy, nếu bạn sử dụng asynctask, bạn phải cẩn thận về cách hủy tác vụ.

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