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

时间:2011-12-28 12:28:56

标签: android geolocation gps gsm

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 Switched ON,那么一旦我们关闭GPS,它就不会切换到GSM获取位置,反之亦然。

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

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

感谢。

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

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

编辑:我修改了我的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);
        }

    }

2 个答案:

答案 0 :(得分:5)

为此找到了解决方案:

 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()。

由于

答案 1 :(得分:0)

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

这是Android博客上关于位置的帖子,它也涵盖了监控提供商的变化:

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