gpt4 book ai didi

android - requestLocationUpdates 从未调用过

转载 作者:行者123 更新时间:2023-11-29 00:25:28 29 4
gpt4 key购买 nike

我只需要一次 GPS 中的实际位置。在此代码中,永远不会调用 onLocationChanged,我不明白为什么。正如我所说:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

它应该每 0 秒和每 0 米运行一次。如何更改代码以使其正常工作?

public class MapActivity extends FragmentActivity implements
LocationListener {

private final String TAG = getClass().getSimpleName();
private GoogleMap mMap;
private Location loc;
private Context ctx;
private radius;
LocationManager mLocationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
ctx = MapActivity.this;
radius = getRadius();

mMap = initMap();

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
loc = location;
Log.v("Location Changed", location.getLatitude() + " and "
+ location.getLongitude());
mLocationManager.removeUpdates(this);
myPlaces = new GetPlaces(ctx, GAS_STATION, mMap, loc, radius);
myPlaces.execute();
Log.e(TAG, "location : " + loc);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

编辑:我将调用移至 loc:

myPlaces = new GetPlaces(ctx, GAS_STATION, mMap, loc, radius);
myPlaces.execute();
Log.e(TAG, "location : " + loc);

在 onLocationChange 中。这样,我就不会再得到 nullpointerException,但我可以等待超过 20 秒才能获取位置。不太好...如果有人知道如何快速修复!

最佳答案

我所做的是在 onCreate 之前添加 2 个监听器,不仅是 gps,还有网络:

在此处找到代码:Location Manager's requestLocationUpdates called only once

private final LocationListener gpsLocationListener =new LocationListener(){

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
final String tvTxt = textView.getText().toString();
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(tvTxt + "GPS available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(tvTxt + "GPS out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(tvTxt + "GPS temporarily unavailable\n");
break;
}
}

@Override
public void onProviderEnabled(String provider) {
textView.setText(textView.getText().toString()
+ "GPS Provider Enabled\n");
}

@Override
public void onProviderDisabled(String provider) {
textView.setText(textView.getText().toString()
+ "GPS Provider Disabled\n");
}

@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(networkLocationListener);
textView.setText(textView.getText().toString()
+ "New GPS location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n");
}
};
private final LocationListener networkLocationListener =
new LocationListener(){

@Override
public void onStatusChanged(String provider, int status, Bundle extras){
final String tvTxt = textView.getText().toString();
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(tvTxt + "Network location available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(tvTxt + "Network location out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(tvTxt
+ "Network location temporarily unavailable\n");
break;
}
}

@Override
public void onProviderEnabled(String provider) {
textView.setText(textView.getText().toString()
+ "Network Provider Enabled\n");
}

@Override
public void onProviderDisabled(String provider) {
textView.setText(textView.getText().toString()
+ "Network Provider Disabled\n");
}

@Override
public void onLocationChanged(Location location) {
textView.setText(textView.getText().toString()
+ "New network location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n");
}
};

并在回调方法中激活它们:

@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, gpsLocationListener);
}

@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(networkLocationListener);
locationManager.removeUpdates(gpsLocationListener);
}

然后效果很好!我认为只有 GPS 是不够的,因为修复可以持续很长时间!

关于android - requestLocationUpdates 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com