I want to know the difference between Android loader and AsyncTask , here the demo on 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;
}
}
}
AsyncTaskLoader is the loader equivalent of AsyncTask .
Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services.
Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread. Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once. Long task in general.
AsyncTaskLoader is used to perform an asynchronous task in the background of the application, so the user can also interact with the application during that process. As soon as the task is completed, the result will be updated to the interface.
AsyncTaskLoader 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.
Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.
AsyncTaskLoader will be just re-used basing on Loader ID that registered in Loader Manager before, so avoid re-executing network transaction. In summary, AsyncTaskLoader prevent duplication of background threads and eliminate duplication of zombie activities. Show activity on this post.
ProgressValue − It contains information about progress units. While doing background operation we can update information on ui using onProgressUpdate (). ResultValue −It contains information about result type. This example demonstrate about how to use asyncTask in android.
From 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.
In my opinion: Loader is better. because I used AsyncTask a year before , that's a really nightmare for me , because you can't control the whole progress immediately , like sometimes , there is also a asynctask run on the activity,but you want to quit the activity ,you should call asynctask.cancel()
, but ,this method : cancel()
is not cancel the asynctask straightway.so in this case ,you application will crash due to this . so , if you use asynctask , you must be careful about how to cancel the task .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With