Answer the question
In order to leave comments, you need to log in
Cyclic data update from a REST service in the background (dataDroid)?
Following the example from the article Writing a simple application for working with RESTful... , I implemented my application that requests news and saves the time of the last update.
I can't get the data to update after a given period of time in the background.
According to the advice on Stack Overflow, I use the Alarm Manager directly from Operation
request.put("id", user_id);
request.put(Tweets.CREATED,
prefs.getString(user_id + "_" + Tweets.CREATED, ""));
Intent intent = new Intent(context, RestService.class);
intent.putExtra(RestService.INTENT_EXTRA_REQUEST, request);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, 0);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
long interval = 1000 * 60 * Integer.parseInt(prefs.getString("interval", "5"));
am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
+ interval, pendingIntent);
void update() {
listView.setRefreshing();
if (!user_id.equals("0")) {
Request updateRequest = new Request(RequestFactory.REQUEST_TWEETS);
updateRequest.put("method", "projects.feed");
updateRequest.put("id", user_id);
updateRequest.put(Tweets.CREATED,
prefs.getString(user_id + "_" + Tweets.CREATED, ""));
requestManager.execute(updateRequest, requestListener);
} else {
listView.onRefreshComplete();
}
}
Answer the question
In order to leave comments, you need to log in
Solved the problem in the forehead. Created another IntentService service with requestListener class independent from MainActivity
@Override
protected void onHandleIntent(Intent arg0) {
String user_id=prefs.getString("user_id", "0");
Request updateRequest = new Request(RequestFactory.REQUEST_TWEETS);
updateRequest.put("method", "projects.feed");
updateRequest.put("id", user_id);
updateRequest.put(Tweets.CREATED,
prefs.getString(user_id + "_" + Tweets.CREATED, ""));
requestManager.execute(updateRequest, requestListener);
}
void startTimer() {
Intent intent = new Intent(context, TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, 0);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
long interval_=1000 * 60 * Integer.parseInt(interval);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
+ interval_, interval_, pendingIntent);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question