【发布时间】:2011-12-28 12:28:55
【问题描述】:
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);
}
}
这是我的代码 sn-p,我的问题是,一旦在 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);
}
}
【问题讨论】:
-
是的,我在这里发现了另一个问题,我的 onProviderEnabled() 没有被调用。
标签: android geolocation gps gsm