From: Marius Gavrilescu Date: Sun, 4 Aug 2013 05:43:08 +0000 (+0300) Subject: Prevent against future use-up-lots-of-mobile-data bugs X-Git-Tag: 0.000_001~9 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=471a079f9631cd86c0f510b1277c80d040b943fb;p=fonbot.git Prevent against future use-up-lots-of-mobile-data bugs --- diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index 8e8d67a..421caa7 100644 --- a/src/ro/ieval/fonbot/FonBotMainService.java +++ b/src/ro/ieval/fonbot/FonBotMainService.java @@ -19,6 +19,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.os.IBinder; +import android.os.SystemClock; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.content.LocalBroadcastManager; @@ -70,14 +71,25 @@ public final class FonBotMainService extends Service { * @author Marius Gavrilescu */ private final class LongPollRunnable implements Runnable{ + /** + * Minimum time between two runs of the long polling service, in milliseconds + * + * This ensures the long polling service won't use up all your mobile data. + */ + private static final int MIN_MILLIS_BETWEEN_RUNS = 10000; + public void run(){ final ConnectivityManager man=(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); final HttpCallExecutableRunnable runnable=new HttpCallExecutableRunnable("/get", null, FonBotMainService.this, new PollResultCallback(FonBotMainService.this), false); Log.d("LongPollRunnable", "Long polling started"); + long lastRun = 0; while(man.getActiveNetworkInfo() != null && man.getActiveNetworkInfo().isConnected()) try { - runnable.run(); + if(lastRun + MIN_MILLIS_BETWEEN_RUNS > System.currentTimeMillis()) + SystemClock.sleep(lastRun + MIN_MILLIS_BETWEEN_RUNS - System.currentTimeMillis()); + else + runnable.run(); } catch (final Exception ex){ ex.printStackTrace(); break;