【发布时间】:2014-04-11 21:49:46
【问题描述】:
当然我的问题是重复的。但我无法为我的解决方案找到好的答案。 我想编写一个自定义通用类,它可以通过位置管理器找到当前位置,并在设备未启用 gps 或网络时自己检测 gps 启用或网络启用或使用缓存和存储的位置。最重要的是在以任何方式找到第一个位置后停止工作。但我为它做了这些工作:1-创建两个位置监听器类(GPSListener 和 networkListner):
public class GPSListener implements LocationListener {
LocationManager locationManager=null;
public Boolean found=false;
public Location mainLocation=null;
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
found=true;
mainLocation= location;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
public class NetworkListener implements LocationListener {
LocationManager locationManager=null;
public Boolean found=false;
public Location mainLocation=null;
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
found=true;
mainLocation=location;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
然后我在我的活动中使用这些类:
LatLng result = null;
final LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (DeviceHelper.IsGPSEnabled()) {
GPSListener gpsListener = new GPSListener();
// if(gpsListener.found)
// {
if (gpsListener.mainLocation != null) {
ToastHelper.Show(this,
"GPS : " + gpsListener.mainLocation.toString());
gpsListener = null;
result = new LatLng(gpsListener.mainLocation.getLatitude(),
gpsListener.mainLocation.getLongitude());
}
// }
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
}
else if (DeviceHelper.HasConnect()) {
NetworkListener netListener = new NetworkListener();
// if(netListener.found)
// {
if (netListener.mainLocation != null) {
ToastHelper.Show(this,
"NETWORK : " + netListener.mainLocation.toString());
netListener = null;
result = new LatLng(netListener.mainLocation.getLatitude(),
netListener.mainLocation.getLongitude());
}
// }
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, netListener);
} else {
;
Location loc = LocationHelper.getLastKnownLoaction(false);
if (loc != null) {
ToastHelper.Show(this, "LAST : " + loc.toString());
result = new LatLng(loc.getLatitude(), loc.getLongitude());
}
}
if (result != null) {
LogHelper.Add("LL", String.valueOf(result.latitude));
LocationHelper.MyLatLng = result;
StaticObjects.mainMap.addMarker(new MarkerOptions().position(result));
MapHelper.CameraGo( result, 15);
} else {
LogHelper.Add("ERR:", "NULL");
}
我知道这行不通,因为在我显示找到的位置之前找不到经理的好时机。完全我想写一个通用类来查找当前位置,当找到第一个位置时可以停止。
【问题讨论】:
-
你有什么问题?
-
如何编写一个通用类来通过 gps、网络和断开模式查找我的位置,并在提供者找到第一个位置时停止管理器?
标签: android location locationmanager