In many scenarios, we need to solve some problems such as rotation. If such an algorithm appears on the app, this rotation is a huge disaster for app performance and power consumption, so how do you solve this problem?
The app uses the exponential backoff algorithm exponential back-off algorithm to reduce the frequency of updates in the event that it has not been used since the last update operation. Here we introduce the exponential backoff algorithm.
Sharedpreferences sp = context.getsharedpreferences (PREFS, context.mode_world_readable);
Boolean appused = Sp.getboolean (prefs_appused, false); long updateinterval = Sp.getlong (Prefs_interval, Default_refresh _interval), if (!appused) if ((updateinterval *= 2) > Max_refresh_interval) updateinterval = Max_refresh_ INTERVAL; Editor SpeDIt = Sp.edit (); Spedit.putboolean (prefs_appused, false); Spedit.putlong (Prefs_interval, updateinterval); Spedit.apply (); rescheduleupdates (updateinterval); Executeupdateorprefetch ();
The cost of initializing a network connection does not change if the data has been successfully downloaded. We can use the exponential backoff algorithm to reduce the number of repeated attempts (retry) to avoid wasting power. For example:
private void Retryin (long interval) { Boolean success = Attempttransfer (); if (!success) { Retryin (interval*2 < max_retry_interval? ) Interval*2:max_retry_interval);} }
Exponential backoff algorithm exponential back-off algorithm