Android loader和AsyncTask之间有什么区别?

Android loader和AsyncTask之间有什么区别?,android,android-asynctask,loader,Android,Android Asynctask,Loader,我想知道Android 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.AsyncTaskLo

我想知道Android 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;
        }

    }

}
package com.android.loaderdemo;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.LoaderManager.LoaderCallbacks;
导入android.content.AsyncTaskLoader;
导入android.content.Context;
导入android.content.Loader;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.widget.TextView;
导入com.android.webprocessor.Http_GetServices;
公共类MainActivity扩展活动实现LoaderCallbacks{
TextView txt_用户名、txt_用户名、txt_密码、txt_角色、txt_秘密问题、txt_答案、txt_密码;
专用静态字符串url=”http://dotstg1.xyz.com/loud/webservcies/GetUserProfile.svc/GetUserProfile/124";
静态字符串响应;
静态字符串名称、电子邮件、密码、答案、RoleId、SecretQuestion、Zip;
静态字符串useResult=null;
静态JSONArray userParams=null;
私有静态final int_加载程序=0x01;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(“onCreate”、“onCreate”);
//getSuLoaderManager().initLoader(_加载程序,null,this.forceLoad();
getLoaderManager();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
Log.e(“onCreateOptions菜单”、“onCreateOptions菜单”);
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共加载器onCreateLoader(int arg0,Bundle arg1){
Log.e(“onCreateLoader”、“onCreateLoader”);
SampleLoader=新的SampleLoader(此);
返回装载机;
}
@凌驾
public void onLoadFinished(加载程序arg0,字符串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(名称);
txt_userEmail.setText(电子邮件);
txt_password.setText(Pasword);
txt_role.setText(RoleId);
txt_secretQuestion.setText(secretQuestion);
txt_answer.setText(答案);
txt_zipcode.setText(Zip);
Log.e(“onLoadFinished”、“onLoadFinished”);
}
@凌驾
公共void onLoaderReset(加载程序arg0){
Log.e(“onLoaderReset”、“onLoaderReset”);
}
私有静态类SampleLoader扩展了AsyncTaskLoader{
@凌驾
公共上下文getContext(){
Log.e(“getContext”、“getContext”);
返回super.getContext();
}
@凌驾
公共int getId(){
Log.e(“getId”、“getId”);
返回super.getId();
}
公共样本加载器(上下文){
超级(上下文);
Log.e(“SampleLoader”、“SampleLoader”);
}
@凌驾
公共字符串loadInBackground(){
Log.e(“加载背景”、“加载背景”);
试一试{
response=Http_GetServices.connect(url);
JSONObject JSONObject=新JSONObject(响应);
JSONObject json2=JSONObject.getJSONObject(“GetUserPrfResult”);
String test=(String)json2.get(“description”);
JSONObject json3=json2.getJSONObject(“GetUserPrfParams”);
name=(String)json3.get(“name”);
email=(字符串)json3.get(“email”);
Pasword=(String)json3.get(“Pasword”);
RoleId=String.valueOf(json3.getInt(“RoleId”);
SecretQuestion=String.valueOf(json3.get(“SecretQuestion”);
Answer=(字符串)json3.get(“Answer”);
Zip=String.valueOf(json3.get(“Zip”);
}
捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}
}
来自:

加载程序的一个子类是AsyncTaskLoader。这门课表演 与AsyncTask的功能相同,但稍好一些。它能处理 活动配置更容易更改,并且在 碎片和活动的生命周期。好在 AsyncTaskLoader可用于AsyncTask不可用的任何情况 正在使用中。任何时候都需要将数据加载到内存中,以便 要处理的活动/片段,AsyncTaskLoader可以执行此任务 更好


在我看来:装载机更好。因为我在一年前使用了AsyncTask,这对我来说真是一场噩梦,因为你不能立即控制整个进度,就像有时,活动上也有一个AsyncTask运行,但是你想退出活动,你应该调用
AsyncTask.cancel()
,但是,这个方法:
cancel()
不会直接取消异步任务。因此,在这种情况下,应用程序将因此而崩溃。因此,如果使用asynctask,则必须小心如何取消该任务。

完全取决于:您的需求、数据库表架构和关系、您的表是否支持自动递增主键以及是否只需显示一个表数据。虽然异步任务最好保存/更新/删除etc操作,但这些操作是短期的。