在Activity而非Application类上实现BootstrapNotifier [英] Implementing BootstrapNotifier on Activity instead of Application class
问题描述
我正在使用altBeacon库进行信标检测.在检出信标检测示例代码后,它们始终在Application
类上实现接口BootstrapNotifier
而不是在后台检测信标的Activity
上实现.但是我注意到,当在Activity
上实现BootstrapNotifier
时,在后台停止信标检测.我不希望我的应用程序在启动后立即检测到信标,因此我没有在Application
类上实现BootStrapNotifier
.我有一个特定的要求,即我希望仅在特定的Activity
被检测到后才能检测到信标发射,然后应始终在后台检测到信标. altBeacon库是否有实现此目的的规定?谢谢.
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.
推荐答案
是的,只有在Activity
开始之后才有可能在后台检测信标,但是您仍然需要创建一个自定义的Application
类来实现BootstrapNotifier
.
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
.
之所以需要这样做,是因为Android的生命周期.可以通过退出Activity
,继续到新的Activity
或通过操作系统在内存不足的情况下终止Activity
来退出它(如果您从前台开始使用它的话).
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.
在内存不足的情况下,整个应用程序(包括Application
类实例(以及信标扫描服务))将与Activity
一起终止.这种情况是完全无法控制的,但是Android Beacon库中的代码会在发生这种情况后的几分钟内自动重新启动应用程序和Beacon扫描服务.
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.
您遇到的棘手部分是要在Application
类的onCreate
方法中弄清楚这是应用程序重启(低内存关闭后)还是在Activity
曾经运行过.
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.
一种好的方法是将时间戳记存储在 SharedPreferences Activity
启动的时间.
A good way to do this is to store a timestamp in SharedPreferences for when your Activity
is launched.
所以您的Application
类可能具有这样的代码:
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);
}
在您的Activity
中,您还将需要代码来检测它是第一次启动,如果是,则从那里开始扫描.
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();
}
}
这篇关于在Activity而非Application类上实现BootstrapNotifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!