如何获得当前GPS位置无论在线或离线在android系统? [英] How to get current gps location both online or offline in android?
问题描述
我想获得当前位置与两个在线或离线GPS的工作,以及对GPS的预期的行为是:
1.如果WiFi或3G网络可用,它会用这些来获取GPS位置。
2.如果没有WiFi或3G网络缴费它会使用平板电脑的离线GPS(如present)
3.如果没有什么是present,它会显示错误消息。
I want to get the current location working with both online or offline GPS as well the expected behavior of the GPS is: 1. If wifi or 3g networks are available, it will use those to fetch the GPS position. 2. If NO wifi or 3g networks are avaiable it will use the OFFLINE GPS of the tablet (if present) 3. If nothing is present, it will display the Error message.
我想了很多,但还没有成功。我正在启用网络总是如此。
I am trying a lot but not succeeded yet. I am getting network enabled always true.
public boolean isGPSEnabled() {
Location location = null;
try {
mLocationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = mLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
return false;
}
} catch (Exception e) {
toast("Exception " + e.toString());
}
return true;
}
如果任何一个有想法。请帮忙。在此先感谢
If any one have idea. Please help. Thanks in advance
推荐答案
而不是一个单一的功能它分成两个功能检查启用哪一个
Instead of a single function split it into two functions to check which one is enabled
即
要检查是否启用GPS
To check if GPS enabled
public boolean isGPSEnabled() {
Location location = null;
try {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
toast("Exception " + e.toString());
}
return true;
}
要检查是否启用WIFI
To check if WIFI enabled
public boolean isWIFIEnabled() {
Location location = null;
try {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
// getting network status
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
toast("Exception " + e.toString());
}
return true;
}
然后,每当你要检查它,试试这个
Then whenever you want to check it,try this
if(isWIFIEnabled())
{
//request location updates from network provider
}
else if(isGPSEnabled())
{
//request location updates from gps provider
}
else
{
//display error
}
这篇关于如何获得当前GPS位置无论在线或离线在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!