Implementing BootstrapNotifier on Activity instead of Application class

痞子三分冷 提交于 2019-12-12 02:55:42

问题


I am using altBeacon library for beacon detection. After checking out the sample codes on Beacon detection, they always implement the Interface BootstrapNotifier on the Application class instead of Activity, which is used for detecting beacons in the background. However I have noticed that beacon detection in the background stops when BootstrapNotifier is implemented on Activity. I don't want my app to detect beacons as soon as it is launched hence I have not implemented BootStrapNotifier on Application class.I have a specific requirement where I want beacons to be detected only after a particular Activity is launched and thereafter the beacons should always be detected in the background. Does the altBeacon library have any provisions for achieving this? Thanks.

Sample code


回答1:


Yes, it is possible to detect beacons in the background only after an Activity starts, but you still need to make a custom Application class that implements the BootstrapNotifier.

The reason this is necessary is because of the Android lifecycle. An Activity may be exited by backing out of it, going on to a new Activity, or by the operating system terminating it in a low memory condition if you have taken it from the foreground.

In the low memory case, the entire application, including the Application class instance (and the beacon scanning service) is terminated along with the Activity. This case is completely beyond your control, but the Android Beacon Library has code to automatically restart the application and the beacon scanning service a few minutes after this happens.

The tricky part for your purposes is to figure out in the onCreate method of the Application class whether this this is an app restart (after low memory shutdown), or a first time launch of the app before the Activity has ever been run.

A good way to do this is to store a timestamp in SharedPreferences for when your Activity is launched.

So your Application class might have code like this:

public void onCreate() {
  ...
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  long lastActivityLaunchTime = prefs.getLong("myapp_activity_launch_time_millis", 0l);
  if (lastActivityLaunchTime > System.currentTimeMillis()-SystemClock.elapsedRealtime() ) {
    // If we are in here we have launched the Activity since boot, and this is a restart
    // after temporary low memory shutdown.  Need to start scanning
    startBeaconScanning();  
  }
}

public void startBeaconScanning() {
    Region region = new Region("backgroundRegion",
            null, null, null);
    mRegionBootstrap = new RegionBootstrap(this, region);
    mBackgroundPowerSaver = new BackgroundPowerSaver(this);      
}

In your Activity you will also need code to detect it is a first launch, and if so, start the scanning from there.

public void onCreate() {
  ...
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  long lastActivityLaunchTime = prefs.getLong("myapp_activity_launch_time_millis", 0l);
  if (lastActivityLaunchTime < System.currentTimeMillis()-SystemClock.elapsedRealtime() ) {
    // This is the first Activity launch since boot
    ((MyApplication) this.getApplicationContext()).startBeaconScanning();
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putLong("myapp_activity_launch_time_millis", System.currentTimeMillis());
    prefsEditor.commit();
  }
}


来源:https://stackoverflow.com/questions/34824283/implementing-bootstrapnotifier-on-activity-instead-of-application-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!