0

LocationListener在我的应用程序中使用 a ,并为它分配了两个提供程序(GPS_PROVIDER和),并且NETWORK_PROVIDER此侦听器有四个回调(onLocationChangedonStatusChanged和)。onProviderEnabledonProviderDisabled

自从我关闭或打开 GPS 后,我对 GPS 没有任何问题,onProviderEnabled并且onProviderDisabled回调工作。问题是当我改变我的 WIFI 状态时,这些回调没有被调用。

我找到了一些关于它的信息,并创建了一个方法来确定设备中是否有互联网,问题是如果我有互联网,我分配NETWORK_PROVIDER然后我关闭我的 wifi 回调最终不会被调用,并且那么我无法管理这个。

我应该怎么做才能正确配置网络提供商?

代码:

分配提供者:

  isGpsEnabled = locationManager.isProviderEnabled(GPS_PROVIDER);
  isNetworkEnabled = locationManager.isProviderEnabled(NETWORK_PROVIDER) && isNetworkAvailable();

  if(!isGpsEnabled && !isNetworkEnabled) {
    this.gpsAware.onGpsError("Your Gps providers are not available");
    throw new Exception("Verify your GPS or Network connection");
  } else {
    if(isNetworkEnabled)
      addProvider(NETWORK_PROVIDER);
    if(isGpsEnabled)
      addProvider(GPS_PROVIDER);

addProvider 方法:

private void addProvider(String provider) {
    //Check permissions
    locationManager.requestLocationUpdates(
        provider,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
}
4

0 回答 0