diff --git a/mobile/android/base/GeckoApp.java b/mobile/android/base/GeckoApp.java index dc9010fb347..743bd09152b 100644 --- a/mobile/android/base/GeckoApp.java +++ b/mobile/android/base/GeckoApp.java @@ -129,7 +129,7 @@ abstract public class GeckoApp public static int mOrientation; private GeckoConnectivityReceiver mConnectivityReceiver; - private BroadcastReceiver mBatteryReceiver; + private GeckoBatteryManager mBatteryReceiver; public static BrowserToolbar mBrowserToolbar; public static DoorHangerPopup mDoorHangerPopup; @@ -1731,15 +1731,13 @@ abstract public class GeckoApp GeckoAppShell.registerGeckoEventListener("Session:StatePurged", GeckoApp.mAppContext); GeckoAppShell.registerGeckoEventListener("Bookmark:Insert", GeckoApp.mAppContext); - IntentFilter batteryFilter = new IntentFilter(); - batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED); - mBatteryReceiver = new GeckoBatteryManager(); - registerReceiver(mBatteryReceiver, batteryFilter); - if (SmsManager.getInstance() != null) { SmsManager.getInstance().start(); } + mBatteryReceiver = new GeckoBatteryManager(); + mBatteryReceiver.registerFor(mAppContext); + mConnectivityReceiver = new GeckoConnectivityReceiver(); mConnectivityReceiver.registerFor(mAppContext); @@ -2088,7 +2086,7 @@ abstract public class GeckoApp super.onDestroy(); - unregisterReceiver(mBatteryReceiver); + mBatteryReceiver.unregisterFor(mAppContext); if (mAboutHomeContent != null) { mAboutHomeContent.onDestroy(); diff --git a/mobile/android/base/GeckoBatteryManager.java b/mobile/android/base/GeckoBatteryManager.java index 0dc7d341dea..0434cb9607d 100644 --- a/mobile/android/base/GeckoBatteryManager.java +++ b/mobile/android/base/GeckoBatteryManager.java @@ -42,10 +42,11 @@ import java.util.Date; import android.util.Log; +import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; - +import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Build; import android.os.SystemClock; @@ -68,6 +69,24 @@ public class GeckoBatteryManager private static boolean sCharging = kDefaultCharging; private static double sRemainingTime = kDefaultRemainingTime;; + private static boolean isRegistered = false; + + public void registerFor(Activity activity) { + if (!isRegistered) { + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_BATTERY_CHANGED); + activity.registerReceiver(this, filter); + isRegistered = true; + } + } + + public void unregisterFor(Activity activity) { + if (isRegistered) { + activity.unregisterReceiver(this); + isRegistered = false; + } + } + @Override public void onReceive(Context context, Intent intent) { if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {