好的,我解決了另一種方式。 我正在使用我的自定義服務,並且沒有在應用程序中實施更多的BootstrapNotifier。
這是我的代碼,如果有人需要它。
public class BeaconDetector extends Service implements BeaconConsumer {
private static final String TAG = "BeaconDetector";
private BeaconUtility.BeaconObject beaconObject;
private Context getServiceCtx(){
return BeaconDetector.this;
}
@Override
public void onCreate() {
super.onCreate();
IMLog.e(TAG, "Created.");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IMLog.e(TAG, "Start.");
beaconObject = BeaconUtility.instantiateBeaconManager(this);
beaconObject.beaconManager.bind(this);
return START_STICKY;
}
@Override
public void onDestroy() {
IMLog.e(TAG, "Destroy.");
beaconObject.beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
IMLog.e(TAG, "Connected.");
beaconObject.beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region arg0) {
// In this example, this class sends a notification to the user whenever a Beacon
// matching a Region (defined above) are first seen.
IMLog.e(TAG, "did enter region.");
Sender.getInstance(getServiceCtx()).startSender();
}
@Override
public void didExitRegion(Region region) {
IMLog.e(TAG, "did exit region.");
if (Sender.getInstance(getServiceCtx()).isAlive()) {
Sender.getInstance(getServiceCtx()).stopSender();
}
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
IMLog.e(TAG, "did enter region.");
}
});
try {
beaconObject.beaconManager.startMonitoringBeaconsInRegion(BeaconUtility.getMonitoringRegion());
} catch (RemoteException e) {
IMLog.e(TAG, "Remote Exception.");
}
}
}
很高興你解決它!與使用RegionBootstrap相比,這是一個更好的方法,它實際上是設計用於'Application'類的。 – davidgyoung