android - loader 和 AsyncTask 有什么区别?

标签 android android-asynctask loader

<分区>

我想知道 Android loader 和 AsyncTask 的区别, 这里是 Loader 的演示:

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

    }

}

最佳答案

来自 here :

One subclass of Loaders is the AsyncTaskLoader. This class performs the same function as the AsyncTask, but a bit better. It can handle Activity configuration changes more easily, and it behaves within the life cycles of Fragments and Activities. The nice thing is that the AsyncTaskLoader can be used in any situation that the AsyncTask is being used. Anytime data needs to be loaded into memory for the Activity/Fragment to handle, The AsyncTaskLoader can do the job better.

关于android - loader 和 AsyncTask 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24778226/

相关文章:

android - 如何在 meteor 中锁定应用程序的方向

android - 为 Dalvik 实现热代码交换

java - 内部类可以访问但不能更新值 - AsyncTask

java - 使用许多 AsyncTasks 会使我的 Android 应用程序运行得更快吗?

linker - 在调用 ELF 的默认入口点之前是否运行任何用户模式代码?

android - 无法在 intellij idea 的设计选项卡中看到 View

android - 客户端验证 - Android

android - 带有 fragment 的异步任务,关闭 VM 时出错

html - 加载屏幕示例

java - flash.display.Loader 间歇性地导致加载的 swf 的子项松散类型