根据可用性在 GPS 和网络提供商之间切换

发布于 2024-12-23 07:03:34 字数 2553 浏览 2 评论 0原文

public void onCreate() {
 locationListener = new GeoUpdateHandler();
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

try {
   gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
   network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) { 
}

if(gps_enabled) {
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 100, locationListener);
} else if(network_enabled) { // Checking for GSM
   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, DISTANCE, locationListener);
}   
}   
public class GeoUpdateHandler implements LocationListener {
private void getPresentLocation(Location location) {
    ... 
            //Fetch the locations
            ...
}

@Override
public void onProviderDisabled(String provider) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
public void onLocationChanged(Location location) {
    getPresentLocation(location);
}
}

这是我的代码片段,我的问题是,一旦应用程序在 GPS 打开的情况下安装,那么一旦我们关闭 GPS,它就不会切换到 GSM 来获取位置,反之亦然。

因此,请告诉我一种根据位置提供商的可用性在 GPS 和 GSM 之间有效切换的方法。

注意:我的应用程序是一项服务。

谢谢。

编辑:我有一个 BroadcastReceiver,这就是为什么我假设一旦 GPS 打开/关闭发生,就会调用 onCreate() 。

我还尝试检查 onLocationChanged(Location location) 中的提供程序可用性。 但这没有帮助。

编辑:我修改了我的 onProviderDisabled & onProviderEnabled 像这样, 请告诉我在这里做错了什么..

    public void onProviderDisabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);               
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        }   
    }

    @Override
    public void onProviderEnabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        } else {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
        }

    }
public void onCreate() {
 locationListener = new GeoUpdateHandler();
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

try {
   gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
   network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) { 
}

if(gps_enabled) {
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 100, locationListener);
} else if(network_enabled) { // Checking for GSM
   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, DISTANCE, locationListener);
}   
}   
public class GeoUpdateHandler implements LocationListener {
private void getPresentLocation(Location location) {
    ... 
            //Fetch the locations
            ...
}

@Override
public void onProviderDisabled(String provider) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
public void onLocationChanged(Location location) {
    getPresentLocation(location);
}
}

this is my code snippet and my issue is that once the application is installed with GPS Switched ON, then it is not switching to GSM to fetch location once we OFF GPS, and vice versa.

So please tell me a way to switch efficiently between GPS and GSM according to the availability of the location provider.

NOTE: My app is a service.

Thanks.

Edit: I got a BroadcastReceiver, thats why i assume the onCreate() will be invoked once the GPS ON/OFF happen.

I also tried checking the provider availability in onLocationChanged(Location location).
But it didnt helped.

EDIT: I modified my onProviderDisabled & onProviderEnabled like this,
Please tell what i am doing wrong here..

    public void onProviderDisabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);               
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        }   
    }

    @Override
    public void onProviderEnabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        } else {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-12-30 07:03:34

找到了解决方案:

 public void onProviderDisabled(String provider) {
   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  locationListener);
   } else {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
   } 
}

@Override
public void onProviderEnabled(String provider) {
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  } else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  }
}

我错过的是
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
在 onProviderEnabled() 和 onProviderDisabled() 中。

谢谢

Got a solution for this:

 public void onProviderDisabled(String provider) {
   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  locationListener);
   } else {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
   } 
}

@Override
public void onProviderEnabled(String provider) {
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  } else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  }
}

what i missed is
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
in onProviderEnabled() and onProviderDisabled().

Thanks

萌︼了一个春 2024-12-30 07:03:34

看起来您只在 onCreate() 方法中请求位置更新。一旦提供商发生变化,您就必须向新提供商请求更新,不是吗?

这是 Android 博客上关于位置的帖子,它也涵盖了监控提供程序的更改:

http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

Looks like you're only requesting location updates in onCreate() method. Once providers change you have to request updates from the new provider, no?

Here's a post on Android blog about locations, it covers monitoring provider changes too:

http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文