AltBeacon 在应用关闭时继续扫描
AltBeacon keep scanning when the app is closed
我正在使用 AltBeacon
信标扫描库来扫描信标。我在一项服务中启动信标扫描仪,该服务在启动时或蓝牙状态发生变化时被广播接收器调用。当我打开应用程序并再次退出时,BeaconHandler
被杀死并重新启动,因为信标服务再次调用广播接收器。
问题是我不希望在退出我的应用程序时重置信标扫描仪,因为我将所有扫描的信标保存在 ArrayLis
t 中,并且需要在一个文件中跟踪用户的移动建造。因此,当我退出应用程序时,我的 Arraylist
被清除,我无法跟踪用户的移动。
我的后台服务:
public class BeaconService extends Service {
private static final String TAG = "BeaconService";
private BeaconHandler beaconHandler;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand()");
beaconHandler = BeaconHandler.getInstance(getApplicationContext(),
BeaconHandler1.getInstance(getApplicationContext()),
BeaconHandler2.getInstance(getApplicationContext()));
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy()");
beaconHandler.stopListening();
sendBroadcast(new Intent(this, BluetoothReceiver.class));
}
}
我的广播接收器:
public class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BluetoothReceiver", "onReceive()");
if (BeaconManager.getInstanceForApplication(context).checkAvailability()) {
context.startService(new Intent(context, BeaconService.class));
} else {
context.stopService(new Intent(context, BeaconService.class));
}
}
}
如您所见,信标处理程序是一个单例class,因此当时只有一个实例运行。有没有办法防止信标扫描器重置并在应用程序关闭时清除我的 ArrayList
?
如果内存不足,您无法阻止应用程序被用户或操作系统终止。
解决方案是将您的应用程序状态保存在非易失性存储器中,然后在服务启动时读回。如果您的数据很简单,SharedPreferences 是一个很好的存储机制。
请记住,您无法控制您的应用程序何时被终止,或者在它发生时依赖回调,因此您需要定期保存数据。
我正在使用 AltBeacon
信标扫描库来扫描信标。我在一项服务中启动信标扫描仪,该服务在启动时或蓝牙状态发生变化时被广播接收器调用。当我打开应用程序并再次退出时,BeaconHandler
被杀死并重新启动,因为信标服务再次调用广播接收器。
问题是我不希望在退出我的应用程序时重置信标扫描仪,因为我将所有扫描的信标保存在 ArrayLis
t 中,并且需要在一个文件中跟踪用户的移动建造。因此,当我退出应用程序时,我的 Arraylist
被清除,我无法跟踪用户的移动。
我的后台服务:
public class BeaconService extends Service {
private static final String TAG = "BeaconService";
private BeaconHandler beaconHandler;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand()");
beaconHandler = BeaconHandler.getInstance(getApplicationContext(),
BeaconHandler1.getInstance(getApplicationContext()),
BeaconHandler2.getInstance(getApplicationContext()));
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy()");
beaconHandler.stopListening();
sendBroadcast(new Intent(this, BluetoothReceiver.class));
}
}
我的广播接收器:
public class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BluetoothReceiver", "onReceive()");
if (BeaconManager.getInstanceForApplication(context).checkAvailability()) {
context.startService(new Intent(context, BeaconService.class));
} else {
context.stopService(new Intent(context, BeaconService.class));
}
}
}
如您所见,信标处理程序是一个单例class,因此当时只有一个实例运行。有没有办法防止信标扫描器重置并在应用程序关闭时清除我的 ArrayList
?
如果内存不足,您无法阻止应用程序被用户或操作系统终止。
解决方案是将您的应用程序状态保存在非易失性存储器中,然后在服务启动时读回。如果您的数据很简单,SharedPreferences 是一个很好的存储机制。
请记住,您无法控制您的应用程序何时被终止,或者在它发生时依赖回调,因此您需要定期保存数据。