gpt4 book ai didi

android - 自动重复 Android Volley 请求的最佳做法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:41 24 4
gpt4 key购买 nike

我已经使 ListView 由 Volley JsonObjectRequest 和 setShouldCache 填充为 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 在每个特定时间段重复它。

最佳答案

解决方案

我找到了使用 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);

关于android - 自动重复 Android Volley 请求的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com