android - Implementing BootstrapNotifier on Activity instead of Application class -


i using altbeacon library beacon detection. after checking out sample codes on beacon detection, implement interface bootstrapnotifier on application class instead of activity, used detecting beacons in background. have noticed beacon detection in background stops when bootstrapnotifier implemented on activity. don't want app detect beacons launched hence have not implemented bootstrapnotifier on application class.i have specific requirement want beacons detected after particular activity launched , thereafter beacons should detected in background. altbeacon library have provisions achieving this? thanks.

sample code

yes, possible detect beacons in background after activity starts, still need make custom application class implements bootstrapnotifier.

the reason necessary because of android lifecycle. activity may exited backing out of it, going on new activity, or operating system terminating in low memory condition if have taken foreground.

in low memory case, entire application, including application class instance (and beacon scanning service) terminated along activity. case beyond control, android beacon library has code automatically restart application , beacon scanning service few minutes after happens.

the tricky part purposes figure out in oncreate method of application class whether this app restart (after low memory shutdown), or first time launch of app before activity has ever been run.

a way store timestamp in sharedpreferences when activity launched.

so application class might have code 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 in here have launched activity since boot, , restart     // after temporary low memory shutdown.  need start scanning     startbeaconscanning();     } }  public void startbeaconscanning() {     region region = new region("backgroundregion",             null, null, null);     mregionbootstrap = new regionbootstrap(this, region);     mbackgroundpowersaver = new backgroundpowersaver(this);       } 

in activity need code detect first launch, , if so, start scanning 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() ) {     // 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();   } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -