Shady Atef Shady Atef - 26 days ago 10
Android Question

what's the best practice to repeat Android Volley requests automatically?

I have made ListView populated by Volley JsonObjectRequest and setShouldCache to false..
Now I want to re-populate it every 5 mins, so what's the best practice to do that ??

I thought about making Handler that manages runnable adding requests to the queue every 5 mins..

UPDATE 1



I have Volley JsonObjectRequest like this

jsObjRequest = new JsonObjectRequest(Request.Method.GET,
"Place Holder for URL ",
null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject jObj) {
// Some Processing Here
}
}

}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
// Some Processing Here

}
});
jsObjRequest.setShouldCache(false);


Then I add it to the RequestQueue

addToRequestQueue(jsObjRequest);


Volley only executes it one time.

My question is if there any method using Volley to repeat it every specific period of time.

Answer

Solution

I found the solution to use IntentService and AlarmManager to repeat the requested task like this

    // The IntentService
    public class AnnouncementIntentService extends IntentService {

    protected void onHandleIntent(Intent intent) {

    CustomRequestQueue.getInstance(getApplicationContext())
            .addToRequestQueue(jsObjRequest);
}}


// The Alarm Manager Code
        AlarmManager alarm = (AlarmManager)ctx
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ctx, AnnouncementIntentService.class);
    PendingIntent pinIntent = PendingIntent.getService(ctx, 0, i,
            0);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis(), 10000L, pinIntent);