【发布时间】:2015-03-24 19:38:18
【问题描述】:
我有这个用于检测位置的代码:
public class LocationDetector implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
public LocationDetector(Context context){
_context = context;
}
public Location getlastloc(){
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public void start(){
locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);
lastloc = getlastloc();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
}
public void stop(){
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
lastloc = location;
Log.d("states", "lat: "+lastloc.getLatitude()+"lat: "+lastloc.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
问题是 onLocationChanged() 仅在手机上启用 gps 选项且仅启用 wifi/移动网络选项时才会触发 onLocationChanged() 不会触发并且不会检测位置。
即使我连接到互联网并且启用了 wifi/移动网络选项,也不会再次检测位置。
所以这个选项是干什么用的?什么时候起作用?
【问题讨论】:
-
我认为您可能将更新请求设置得过大。尝试使用小于 400 的值。
-
我现在改成 0 还是不给我位置