我有以下代码,但其中存在问题,因为有时GPS定位需要很长时间。
我该如何做到以下几点:
- 检查GPS是否已启用
- 如果GPS已启用,则使用GPS;否则使用网络提供程序。
- 如果GPS耗时超过30秒,则使用网络。
我可以使用自己的逻辑来完成此操作,例如使用time或Thread.sleep,但我认为可能有更标准的方法。
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
locationCallback(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);