2014-07-16 34 views
7

我想知道在装载机的Android装载机的AsyncTask, 这里演示的区别:loader或AsyncTask有什么区别?

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

完全依赖于:您的需求,数据库表模式和关系,如果您的表支持自动增加主键并且只有一个表数据必须显示Loader是合适的。虽然异步任务最好是保存/更新/删除等操作,但这些操作是短暂的。 –

+0

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

回答

8

here

装载机

一个子类是AsyncTaskLoader。这个类执行 与AsyncTask相同的功能,但好一点。它可以更轻松地处理 活动配置更改,并且它在分段和活动的生命周期内运行。好的是,AsyncTaskLoader可用于任何使用AsyncTask为 的情况。任何时候需要将数据加载到活动/片段处理的内存中,AsyncTaskLoader可以更好地完成 作业。

5

在我看来:装载机比较好。因为我在一年前使用了AsyncTask,对我来说这真是一场噩梦,因为你无法立即控制整个进度,有时候,活动上也有一个asynctask运行,但你想退出活动,你应该打电话给asynctask.cancel(),但是,此方法:cancel()不会取消asynctask,因此在这种情况下,您的应用程序将因此崩溃。所以,如果你使用asynctask,你必须小心如何取消任务。