G
G
Gariks2013-04-23 20:32:22
Java
Gariks, 2013-04-23 20:32:22

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

^ the problem is that am.cancel does not work (after all, the Intent is already different) and as a result, you can receive the same message indefinitely.
In the course of my research, I realized that I need to execute the update method from MainActivity on a timer
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();
    }
  }

but is it possible? And if so, how can it be achieved?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gariks, 2013-04-24
@Gariks

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

and a timer in MainActivity that is reloaded on onStart or after manually updating the list.
  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);
  }

Accordingly, if in the future you have to add functionality, you can pass extras to the service, and in it already, for example, a request factory.

A
Andrey M, 2014-06-21
@Collosteam

It is better to use Sync Adapter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question