什么是自动重复Android排球请求的最佳做法?

时间:2014-09-01 23:23:11

标签: android android-volley

我已将Volley JsonObjectRequest和setShouldCache填充的ListView填充为false。 现在我想每5分钟重新填充一次,那么最好的做法是什么?

我考虑过让Handler每5分钟管理一次可运行的队列添加请求。

更新1

我有像这样的Volley JsonObjectRequest

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

然后我将它添加到RequestQueue

              addToRequestQueue(jsObjRequest);

Volley只执行一次。

我的问题是,是否有任何方法使用Volley在每个特定时间段重复它。

1 个答案:

答案 0 :(得分:0)

解决方案

我找到了使用IntentService和AlarmManager来重复所请求任务的解决方案,如下所示

    // 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);