How are you sending your activity to the background? Are you hitting the back button, the home button or what?
I suspect what is happening is that Android is actually terminating your App. Your onDestroy
method looks like this:
@Override
protected void onDestroy() {
super.onDestroy();
iBeaconManager.unBind(this);
}
If Android calls this method, the iBeaconManager
will unbind to the AndroidIBeaconService
effectively stopping scanning. Even if you remove this code, Android will still kill the service automatically if it decides to terminate the app.
If you want to keep this running in the background, you need to attach the IBeaconManager
to something that has a longer life cycle than that activity. The easiest way to do this is with a custom Android Application class like this (which must also be declared in your manifest):
public class MyTestIBeaconApplication extends Application {
private BackgroundPowerSaver backgroundPowerSaver;
private IBeaconManager iBeaconManager;
public void onCreate() {
super.onCreate();
// Simply constructing this class and holding a reference to it
// enables auto battery saving of about 60%
backgroundPowerSaver = new BackgroundPowerSaver(this);
iBeaconManager = IBeaconManager.getInstanceForApplication(this);
}
}
The BackgroundPowerSaver
part (only in the Pro version of the library) is optional, but will automatically slow down the scans frequency when your app is in the background to save battery. If you use this, you no longer have to make calls to setBackgroundMode
in your various onPause
and onResume
methods.