从LocationManager Android GPS中删除侦听器

从LocationManager Android GPS中删除侦听器,android,gps,Android,Gps,基本上,当GPS找不到信号时,我试图阻止它移动,这其中有一些原因,但不是我想做的 我在服务中设置了以下内容 private void grabsensor() { this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.removeUpdates(this); List<String> enabledProvider

基本上,当GPS找不到信号时,我试图阻止它移动,这其中有一些原因,但不是我想做的

我在服务中设置了以下内容

private void grabsensor() {
    this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(this);
    List<String> enabledProviders = this.locationManager.getProviders(true);

    for (String provider:enabledProviders){
        this.locationManager.requestLocationUpdates(provider, 5000, 0, this);
    }
}
private传感器(){
this.locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationManager.RemoveUpdate(此);
List enabledProviders=this.locationManager.getProviders(true);
for(字符串提供程序:enabledProviders){
this.locationManager.RequestLocationUpdate(提供程序,5000,0,this);
}
}
我正在尝试设计我的应用程序,这样当它以低于30米的精度读取网络时,它就不会扫描GPS,因为在我的使用案例中,用户在建筑物内,这是我节省电池的方法

所以我试着做了以下几点:

    // Smart location handling algorithm
    if ((int) location.getAccuracy() < 30) {
        this.locationManager.removeUpdates(this);
    }
//智能位置处理算法
if((int)location.getAccurance()<30){
this.locationManager.removeUpdates(this);
}

除了这将删除所有提供程序外,我只想删除GPS提供程序,然后下次调用它时,它将检查相同的if语句,如果它为false,它将重新添加GPS提供程序。

您可以为每种提供程序类型创建新的侦听器实例。根据文档,并没有任何方法来检查侦听器是否已注册

class MyLocationListener implements LocationListener {
    //..
}

private LocationListener locationListenerGPS = new MyLocationListener();

private LocationListener locationListenerOther = new MyLocationListener();

private void grabsensor() {
    this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(this);
    List<String> enabledProviders = this.locationManager.getProviders(true);

    for (String provider:enabledProviders) {
        if (provider == LocationManager.GPS_PROVIDER)
            this.locationManager.requestLocationUpdates(provider, 5000, 0, locationListenerGPS);
        else
            this.locationManager.requestLocationUpdates(provider, 5000, 0, locationListenerOther);
    }
}

private void removeGPSListener() {
    if ((int) location.getAccuracy() < 30) {
        this.locationManager.removeUpdates(locationListenerGPS);
    }
}
类MyLocationListener实现LocationListener{
//..
}
private LocationListener locationListenerGPS=新建MyLocationListener();
私有LocationListener locationListenerOther=新建MyLocationListener();
专用传感器(){
this.locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationManager.RemoveUpdate(此);
List enabledProviders=this.locationManager.getProviders(true);
for(字符串提供程序:enabledProviders){
if(provider==LocationManager.GPS\u provider)
this.locationManager.RequestLocationUpdate(提供程序,5000,0,locationListenerGPS);
其他的
this.locationManager.RequestLocationUpdate(提供程序,5000,0,locationListenerOther);
}
}
私有void removeGPSListener(){
if((int)location.getAccurance()<30){
this.locationManager.RemoveUpdate(locationListenerGPS);
}
}

谢谢,我该怎么做?