0

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();
        }
    }
}
anuradha
  • 692
  • 1
  • 9
  • 22
  • If you just want the reload to happen while the user is inside your app, you can just start a thread which triggers the reload function every x seconds/minutes. You can also use JobScheduler, Alarm, or BroadcastReceivers. There are many ways. – Christian Stengel Sep 09 '16 at 15:26
  • how should I restart a thread after x seconds/minutes? – anuradha Sep 09 '16 at 15:29

2 Answers2

1

FireBase Cloud Messenging is designed as a way for a server to notify a client of new data, or at least that it should contact the server for new data.

Eugene Styer
  • 469
  • 3
  • 11
-1

Very minimalistic version of the reload could be something like which would call your function every second. It depends on what your final goal is, this can be super easy or difficult.

// Init
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        yourfunction();
        handler.postDelayed(this, 1000);
    }
};

//Start
handler.postDelayed(runnable, 1000);
  • One problem with the app checking in with the server frequently is that is uses up lots of battery – Eugene Styer Sep 09 '16 at 16:42
  • if your using background services android will handle this for you, not pulling data when in battery saving mode, at night and so on I`m sure there´re best practice tuts on the android developer page – Christian Stengel Sep 13 '16 at 09:27