1.停止监听网络位置更新:
这样做非常简单。只需调用
your_loc_manager.removeUpdates(network_location_listener);
2. 如果 GPS 提供商未提供任何修复,则从网络提供商获取位置
您可以尝试以下给出的内容(而不是计时器,您可以尝试使用 GpsStatus 侦听器...)
在获取网络提供商更新的方法中
boolean network_enabled ;
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
if(!network_enabled)
return;
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, networkLocationListener);
return;
获取 GPS 位置的方法内部
boolean gps_enabled;
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
if(!gps_enabled) {
// call your method for getting network location updates
return;
}
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 50, locationListenerGps);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// You should call your method of getting Network locations based on some
// condition which would tell whether you really need to ask for network location
}
}, 20000 /* Time Value after which you want to stop trying for GPS and switch to Network*/);
编辑1:
另外“我不需要知道何时从 GPS 获取更新,我需要知道何时从网络获取更新(我的意思是我需要知道 gps 何时搜索,而不是何时修复)。以及如何控制网络与GPS的互换性"
...如果修复已丢失,那么您只需要查找网络更新,所以我建议致电
if (hasGPSFix) {
Log.i("GPS","Fix Lost (expired)");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener); //===> new line which I am suggesting
}
hasGPSFix = false;
这样,您将从网络获取位置更新,而 GPS 将继续尝试修复。一旦实现 GPS 定位,hasGPSFix 将更改其值,并且所述链接中的现有代码将停止寻找网络更新。至少这是我能理解的你想要实现的。可能我还没有完全理解你。
编辑 2:
这是我处理 GPS 和网络提供商的课程
mapActivity 或者任何你需要获取位置更新的地方,你都需要创建这个MyLocation
类的一个实例。此外,为了继续获取您需要LocationResult
在呼叫活动中实施的位置更新。public abstract void gotLocation(Location location, boolean isGpsData);
会告诉你刚才收到的更新是来自网络还是 GPS。此外,如果从 GPS 返回数据,它会自动关闭网络侦听器。getFineLocation()
如果您想要来自 GPS 等的数据,您将需要调用方法。您将需要为您的目的增强给定的代码(例如,closeListeners()
当您只想删除网络更新等时,您需要增强仅关闭一个提供程序的方法),但它提供了您想要的基本实现(随着GpsStatus.Listener
实施,当然,所以两者结合起来应该很好地满足您的目的)。
public class MyLocation {
LocationManager locManager;
LocationResult locationResult;
boolean gps_enabled = false;
boolean network_enabled = false;
public boolean getCoarseLocation(Context context, LocationResult result) {
locationResult = result;
if(locManager == null)
locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
if(!network_enabled)
return false;
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, networkLocationListener);
return true;
}
public boolean getFineLocation(Context context, LocationResult result) {
locationResult = result;
if(locManager == null)
locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {
}
if(!gps_enabled)
return false;//No way to get location data
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, locationListenerGps);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
locationResult.gotLocation(location,true);
locManager.removeUpdates(networkLocationListener);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener networkLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
locationResult.gotLocation(location,false);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
public void closeListeners() {
if(locManager == null)
return;
if(locationListenerGps != null)
locManager.removeUpdates(locationListenerGps);
if(networkLocationListener != null)
locManager.removeUpdates(networkLocationListener);
}
public static abstract class LocationResult {
public abstract void gotLocation(Location location, boolean isGpsData);
}
public static boolean hasNetworkProvider(Context mContext) {
List<String> myProvidersList = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getProviders(false);
return myProvidersList.contains("network");
}
public static boolean hasGPSProvider(Context mContext) {
List<String> myProvidersList = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getProviders(false);
return myProvidersList.contains("gps");
}
}