I
I
IvanOne2016-03-25 22:01:03
Android
IvanOne, 2016-03-25 22:01:03

Why are requests looping?

Hello everyone, please help me figure out one thing, the application uses retrofit2, with its help I make a head request to the server

ShoppingService service = ServiceGenerator.createServiceToken(ShoppingService.class, token);
Call is_update_request = service_gen.is_update();
Headers headers = is_update_request.execute().headers();

This is the call
here is the service itself
@HEAD("shoplists_update/")
    Call<Void> is_update();

That is, the header is simply checked there. The
whole thing is launched through the IntentService
@Override
    protected void onHandleIntent(Intent intent) {
        String token = getToken();
        ShoppingService service = ServiceGenerator.createServiceToken(ShoppingService.class, token);
        Log.d(LOG_TAG, "onHandleIntent start ");
        try {
            Log.v(LOG_TAG, "start request");
            if(is_sync(service)){
                Log.v(LOG_TAG, "start sync");
                sync(service);
                Log.v(LOG_TAG, "end sync");
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        Log.d(LOG_TAG, "onHandleIntent end ");
    }

is_sync just makes requests
And the whole thing is scheduled to run in the Activity
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_list);
        AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent(this, SyncService.class);
        PendingIntent pendingIntent=PendingIntent.getService(this, 0, notificationIntent, 0);
        mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), 120, pendingIntent);
    }

The question is that with each launch of the IntentService, the number of requests increases by one, that is, at the first launch one request, at the second two, etc. Why is this happening?
And another question is that the launch of the IntentService is done in the base activity, that is, all other activities are inherited from it, how correct is this? And if not, how to correctly call the IntentService on a schedule.
Please do not throw stones, I'm just starting to write on android.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-03-25
@zagayevskiy

From what I understand, your problem is that you are setting an "alarm" every time onCreate() is called. You need somewhere (for example, Preferences) to remember what you have already set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question