我創建了一個應用程序,該信號範圍信標並計算超過三個信標範圍時的位置。當新的Android Intent開啓時,測距信號停止
問題是,當我想顯示位置時,有必要啓動一個新的Intent,因此主要活動並不在Foreground中。大約5秒後,信標的測距停止,我的位置計算也因爲信標的距離不再變化。
有沒有可能繼續測距信標?我試着在我的主要活動中開始一個異步任務,但它不起作用,也許有一個錯誤,我不知道。
這裏是我的異步任務的代碼和OnBeaconServiceConnect():
public class Monitor_Screen extends Activity implements BeaconConsumer,
SensorEventListener {
private class asyncThread extends AsyncTask<Activity, Void, BeaconManager> {
@Override
protected BeaconManager doInBackground(Activity... arg0) {
// TODO Auto-generated method stub
Thread.currentThread().setName("BeaconManagerThread");
Application myApplication = new Application();
// BeaconManager
myBeaconManager = BeaconManager.getInstanceForApplication(myApplication);
myBeaconManager
.getBeaconParsers()
.add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
myBeaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1));
myBeaconManager.bind(Monitor_Screen.this);
// Region
myRegion = new Region("all beacons", null, null, null);
startService(Monitor_Screen.this.getIntent());
return myBeaconManager;
}
}
@Override
public void onBeaconServiceConnect() {
// // range beacons on connect
try {
myBeaconManager.startRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) {
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
myBeaconManager.setRangeNotifier(new RangeNotifier() {
public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
Region myRegion) {
if (beacons.size() > 0) {
Iterator<Beacon> myIterator = beacons.iterator();
while (myIterator.hasNext()) {
Beacon tempBeacon = myIterator.next();
MyBeacon myBeacon = new MyBeacon(tempBeacon.getId1(),
tempBeacon.getId2(), tempBeacon.getId3(),
tempBeacon.getBluetoothAddress(), tempBeacon
.getRssi(), tempBeacon.getDistance(),
tempBeacon.getTxPower(), System
.currentTimeMillis(),
new Position(0, 0));
boolean isIn = false;
for (MyBeacon blub : myVector) {
if (blub.getBTAdress().equals(
myBeacon.getBTAdress())) {
isIn = true;
blub.distance = myBeacon.getDistance();
}
}
if (!isIn)
myVector.add(myBeacon);
}
}
logBeaconData();
for (int i = 0; i < myVector.size(); i++) {
if (System.currentTimeMillis()
- myVector.get(i).getTimeStamp() > 10000) {
myVector.remove(i);
}
}
}
});
try {
myBeaconManager.startMonitoringBeaconsInRegion(myRegion);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myBeaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didExitRegion(Region arg0) {
// TODO Auto-generated method stub
}
@Override
public void didEnterRegion(Region arg0) {
// TODO Auto-generated method stub
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// TODO Auto-generated method stub
}
});
}
不幸的是,這種解決方案不適合我。我開始了一個新項目並測試了ReferenceApplication,但它不起作用。應用程序崩潰:( – 2014-10-30 09:25:28
嘗試運行參考應用程序的未修改版本。如果看到崩潰,請將鏈接粘貼到堆棧跟蹤。 – davidgyoung 2014-10-30 12:04:48