讓我首先承認搜索這個特定問題時有許多結果。然而,根據這些答案,作品我沒有嘗試過。有時我得到的位置,有時我沒有。如果我使用Location
啓用/禁用,則撥打getLastKnownLocation
時我總是會收到null
。如果我有時手動撤消Location
權限並重新啓用它,它就會起作用。我試過將LocationManager
更改爲location = locationManager .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
,但它不起作用。getLastKnownLocation有時返回null
基本上,我做了以下內容:
public class GPSTracker implements LocationListener {
public static final int LOCATION_SETTINGS_REQUEST_CODE = 109;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60;
private static boolean isGpsStatusChanged = false;
private static AlertDialog.Builder alertDialog;
private LocationManager locationManager;
private boolean canGetLocation = false;
private Location location;
private double latitude;
private double longitude;
private NewLocationListener newLocationListener;
public interface NewLocationListener {
void onLocationChanges(Location location);
}
public GPSTracker() {
}
@SuppressWarnings("all")
public Location getLocation() {
try {
locationManager = (LocationManager) BaseApplication.getContext().getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPSTracker", "Network");
if (locationManager != null) {
Log.d("GPSTracker", "Network - location manager != null");
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.d("GPSTracker", "Network - last known location != null");
latitude = location.getLatitude();
longitude = location.getLongitude();
newLocationListener.onLocationChanges(location);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPSTracker", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("LastKnownLocation", "Location: " + locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
newLocationListener.onLocationChanges(location);
}
}
}
}
}
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
return location;
}
public static boolean isGpsEnabled() {
LocationManager locationManager = (LocationManager) BaseApplication.getContext().getSystemService(Context
.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
@SuppressWarnings("all")
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void setNewLocationListener(NewLocationListener newLocationListener) {
this.newLocationListener = newLocationListener;
}
public void removeLocationListener() {
this.newLocationListener = null;
}
@SuppressWarnings("InlinedApi")
public void showSettingsAlert(final Context context) {
if (alertDialog != null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alertDialog = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
alertDialog = new AlertDialog.Builder(context);
}
alertDialog.setTitle(R.string.gps_window_title);
alertDialog.setMessage(R.string.gps_window_message);
alertDialog.setPositiveButton(R.string.gps_window_button_positive, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
((Activity) context).startActivityForResult(intent, LOCATION_SETTINGS_REQUEST_CODE);
}
});
alertDialog.setNegativeButton(R.string.gps_window_button_negative, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show().setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
alertDialog = null;
}
});
}
@Override
public void onLocationChanged(Location location) {
if (newLocationListener != null) {
newLocationListener.onLocationChanges(location);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public static boolean isGpsStatusChanged() {
return isGpsStatusChanged;
}
public static void setIsGpsStatusChanged(boolean isGpsStatusChanged) {
GPSTracker.isGpsStatusChanged = isGpsStatusChanged;
}
}
然後在我的fragment
:
private void statGPSLocationTracking() {
Log.d(Constants.TAG, "start gps update");
// check if GPS enabled
gpsTracker = new GPSTracker();
gpsTracker.setNewLocationListener(newLocationListener);
gpsTracker.getLocation();
if (!gpsTracker.canGetLocation()) {
Log.d(Constants.TAG, "GPS not enabled, show enable dialog");
// stop the gps tracker(removes the location update listeners)
gpsTracker.stopUsingGPS();
// ask user to enable location
gpsTracker.showSettingsAlert(getActivity());
}
}
private GPSTracker.NewLocationListener newLocationListener = new GPSTracker.NewLocationListener() {
@Override
public void onLocationChanges(Location loc) {
if (getActivity() == null) {
return;
}
// GPS location determined, load the web page and pass the coordinates in the header location section
Log.d(Constants.TAG, "Location listener onLocationChanges: " + loc);
infoManager.setCurrentLocation(loc);
checkRadioInfoNeeded();
// stop the gps tracker(removes the location update listeners)
gpsTracker.stopUsingGPS();
}
};
[LocationManager的getLastKnownLocation的可靠性如何以及它更新的頻率如何?](https://stackoverflow.com/questions/7423624/how-reliable-is-locationmanagers-getlastknownlocation-and-how-often-is -it-updat) – j4rey89