问题描述
- 如何检查 LocationManager.NETWORK_PROVIDER 是否可用?
-
如何检查 LocationManager.NETWORK_PROVIDER 是否可用?我在 AndroidManifest.xml 里面配置了,但是需要在代码里检查一下使用 GPS_PROVIDER 是否可行。如何检查?请大家指点迷津。
解决方案
LocationManager lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.isProviderEnabled(aLocationManager.GPS_PROVIDER);
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
解决方案二:
使用 Network provider 或者 GPS provider 来获取
private Location getLocation() {
Location gpslocation = null;
Location networkLocation = null;
if(locMan==null){
locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
}
try {
if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);
gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener);
networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} catch (IllegalArgumentException e) {
//Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString());
Log.e("error", e.toString());
}
if(gpslocation==null && networkLocation==null)
return null;
if(gpslocation!=null && networkLocation!=null){
if(gpslocation.getTime() < networkLocation.getTime()){
gpslocation = null;
return networkLocation;
}else{
networkLocation = null;
return gpslocation;
}
}
if (gpslocation == null) {
return networkLocation;
}
时间: 2024-10-07 09:02:45