Android 新安卓意向开启时,测距信标停止

Android 新安卓意向开启时,测距信标停止,android,android-asynctask,ibeacon,ibeacon-android,altbeacon,Android,Android Asynctask,Ibeacon,Ibeacon Android,Altbeacon,我已经创建了一个应用程序,它可以对信标进行测距,并在测距超过三个信标时计算位置 问题是,当我想要显示位置时,有必要开始一个新的意图,因此主要活动不在前景中。大约5秒钟后,信标的测距停止,我的位置也在计算,因为信标的距离不再改变 是否有可能继续对信标进行测距?我试着在我的主要活动中启动一个异步任务,但它不起作用,可能是出错了,我不知道 以下是我的异步任务和OnBearConserviceConnect()的代码: public class Monitor\u Screen扩展活动实现Beacons

我已经创建了一个应用程序,它可以对信标进行测距,并在测距超过三个信标时计算位置

问题是,当我想要显示位置时,有必要开始一个新的意图,因此主要活动不在前景中。大约5秒钟后,信标的测距停止,我的位置也在计算,因为信标的距离不再改变

是否有可能继续对信标进行测距?我试着在我的主要活动中启动一个异步任务,但它不起作用,可能是出错了,我不知道

以下是我的异步任务和OnBearConserviceConnect()的代码:

public class Monitor\u Screen扩展活动实现Beaconsumer,
SensorEventListener{
私有类asyncThread扩展了AsyncTask{
@凌驾
受保护的BeaconManager后台(活动…arg0){
//TODO自动生成的方法存根
Thread.currentThread().setName(“BeaconManagerThread”);
应用程序myApplication=新应用程序();
//信标管理器
myBeaconManager=BeaconManager.getInstanceForApplication(myApplication);
myBeaconManager
.getBeaconParsers()文件
.add(新BeaconParser()
.SetbeActionLayout(“m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24”);
myBeaconManager.setBackgroundScanPeriod(时间单位:秒。toMillis(1));
myBeaconManager.bind(Monitor\u Screen.this);
//区域
myRegion=新区域(“所有信标”,null,null,null);
startService(Monitor_Screen.this.getIntent());
返回myBeaconManager;
}
}
@凌驾
beacerviceconnect()上的公共无效{
////连接上的范围信标
试一试{
myBeaconManager.StarTrangBeaconRegion(myRegion);
}捕获(远程异常){
Toast.makeText(getApplicationContext(),e.getMessage(),
Toast.LENGTH_LONG).show();
}
myBeaconManager.setRangeNotifier(新的RangeNotifier(){
公共范围信标区域(收集信标,
地区(我的地区){
如果(beacons.size()>0){
迭代器myIterator=beacons.Iterator();
while(myIterator.hasNext()){
Beacon tempBeacon=myIterator.next();
MyBeacon MyBeacon=新建MyBeacon(tempBeacon.getId1(),
tempBeacon.getId2(),tempBeacon.getId3(),
tempBeacon.getBluetoothAddress(),tempBeacon
.getRssi(),tempBeacon.getDistance(),
tempBeacon.getTxPower(),系统
.currentTimeMillis(),
新位置(0,0));
布尔isIn=false;
用于(MyBeacon blub:myVector){
如果(blub.getbtAddress())等于(
myBeacon.getBTAddress()){
isIn=真;
blub.distance=myBeacon.getDistance();
}
}
如果(!isIn)
myVector.add(myBeacon);
}
}
logBeaconData();
对于(int i=0;i10000){
myVector.remove(i);
}
}
}
});
试一试{
myBeaconManager.startMonitoringBeaconsInRegion(myRegion);
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
myBeaconManager.setMonitorNotifier(新的MonitorNotifier(){
@凌驾
公共区域(区域arg0){
//TODO自动生成的方法存根
}
@凌驾
公共区域(区域arg0){
//TODO自动生成的方法存根
}
@凌驾
public void dideterministateforregion(内部arg0,区域arg1){
//TODO自动生成的方法存根
}
});
}

让信标测距在后台继续的最简单方法是在自定义的
android.app.Application
类中设置信标测距,并让它将回调转发到您的
活动
,以仅在该
活动
位于前台时更新显示

您可以在此处的参考应用程序中看到这样做的示例:

下面是自定义
android.app.Application
类的摘录,在该类中进行了设置。请注意,测距设置一次,并将在应用程序的整个生命周期内继续。测距回调将传递给
mRangingActivity
(如果存在),因此它可以执行任何必要的UI处理

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier {
...

    public void onCreate() {
        mAllBeaconsRegion = new Region("all beacons", null, null, null);
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBackgroundPowerSaver = new BackgroundPowerSaver(this);     
        mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
        if (mRangingActivity != null) {
            mRangingActivity.didRangeBeaconsInRegion(arg0, arg1);
        }

    }

    @Override
    public void didEnterRegion(Region arg0) {
        try {
            Log.d(TAG, "entered region.  starting ranging");
            mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
            mBeaconManager.setRangeNotifier(this);
        } catch (RemoteException e) {
            Log.e(TAG, "Cannot start ranging");
        }
    }

...
}
公共类BeaconReferenceApplication扩展应用程序实现BootstrapNotifier、RangeNotifier{
...
public void onCreate(){
mAllBeaconsRegion=新区域(“所有信标”,null,null,null);
mBeaconManager=BeaconManager.getInstanceForApplication(此应用程序);
mBackgroundPowerSaver=新的BackgroundPowerSaver(此);
mRegionBootstrap=newregionbootstrap(这个,mAllBeaconsRegion);
}
@凌驾
public void didRangeBeanConsincinRegion(集合arg0,区域arg1){
if(活动!=null){
mRangingActivity.didRangeBeanConsincin区域(arg0,arg1);
}
}
@凌驾
公共区域(区域arg0){
试一试{
Log.d(标记“输入区域.开始测距”);
MBEACON经理,StarTrangbeaconregion(Mallbeaconregion);
mBeaconManager.setRangeNotifier(此);
}捕获(远程异常){
Log.e(标记“无法开始测距”);
public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier {
...

    public void onCreate() {
        mAllBeaconsRegion = new Region("all beacons", null, null, null);
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBackgroundPowerSaver = new BackgroundPowerSaver(this);     
        mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
        if (mRangingActivity != null) {
            mRangingActivity.didRangeBeaconsInRegion(arg0, arg1);
        }

    }

    @Override
    public void didEnterRegion(Region arg0) {
        try {
            Log.d(TAG, "entered region.  starting ranging");
            mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
            mBeaconManager.setRangeNotifier(this);
        } catch (RemoteException e) {
            Log.e(TAG, "Cannot start ranging");
        }
    }

...
}