I developed an android app which load data from server. But the app loads data from server when user press the load button.
But I need to show real time data without pressing the reload button. For example; Gmail app shows new emails without pressing any UI.
How can I do this in my app?
Right now I am using AsyncTask
to access the server like the following way
public class HttpAsyncTaskAccountInfo extends AsyncTask<String, Void, String> {
public static final String XXXXX = "XXXXX_XXXX";
private final Activity context;
SharedPreferences XXXX_XXXXX = null;
private ProgressDialog dialog;
private String finder_email = null, finder_name = null;
public HttpAsyncTaskAccountInfo(Activity activity) {
context = activity;
dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
dialog.setMessage("Checking Account Info, please wait.");
dialog.show();
}
@Override
protected String doInBackground(String... urls) {
return Get.GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
try {
JSONArray jArray = new JSONArray(result);
JSONObject jObj = jArray.getJSONObject(0);
Log.d("JSON_STRING", jObj.toString());
String jsonTotalSpent = jObj.getString("total_spent_till_now");
String jsonTotal_spent_with_validity = jObj.getString("total_spent_with_validity");
String jsonHas_paid = jObj.getString("has_paid");
if (jsonHas_paid.equals("true"))
Globals.account_has_paid = "Paid";
else Globals.account_has_paid = "Not Paid";
Log.d("JSON_STRING", jsonTotalSpent);
Log.d("JSON_STRING", jsonTotal_spent_with_validity);
Log.d("JSON_STRING", jsonHas_paid);
Log.d("JSON_STRING", Globals.account_has_paid);
Globals.account_total_spent = jsonTotalSpent;
Globals.account_total_spent_with_validity = jsonTotal_spent_with_validity;
} catch (JSONException e) {
e.printStackTrace();
}
}
}