gpt4 book ai didi

android - 如何在 Activity 中使用 Android Location Helper 类

转载 作者:行者123 更新时间:2023-11-30 04:58:36 24 4
gpt4 key购买 nike

我想在多个 Activity 中访问设备的当前位置。目前我在一个 Activity 中使用 Google FusedLocationProvider。我正在研究如何使用 util/helper 类来获取设备的当前位置以用于不同的 Activity 。我找到了一个帮助程序类,但不确定如何从 Activity 中使用它。请帮助我朝着正确的方向前进。

定位器.java

public class Locator implements LocationListener {

static private final String LOG_TAG = "locator";

static private final int TIME_INTERVAL = 100; // minimum time between updates in milliseconds
static private final int DISTANCE_INTERVAL = 1; // minimum distance between updates in meters

static public enum Method {
NETWORK,
GPS,
NETWORK_THEN_GPS
}

private Context context;
private LocationManager locationManager;
private Locator.Method method;
private Locator.Listener callback;

public Locator(Context context) {
super();
this.context = context;
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}

public void getLocation(Locator.Method method, Locator.Listener callback) {
this.method = method;
this.callback = callback;
switch (this.method) {
case NETWORK:
case NETWORK_THEN_GPS:
Location networkLocation = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLocation != null) {
Log.d(LOG_TAG, "Last known location found for network provider : " + networkLocation.toString());
this.callback.onLocationFound(networkLocation);
} else {
Log.d(LOG_TAG, "Request updates from network provider.");
this.requestUpdates(LocationManager.NETWORK_PROVIDER);
}
break;
case GPS:
Location gpsLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
Log.d(LOG_TAG, "Last known location found for GPS provider : " + gpsLocation.toString());
this.callback.onLocationFound(gpsLocation);
} else {
Log.d(LOG_TAG, "Request updates from GPS provider.");
this.requestUpdates(LocationManager.GPS_PROVIDER);
}
break;
}
}

private void requestUpdates(String provider) {
if (this.locationManager.isProviderEnabled(provider)) {
if (provider.contentEquals(LocationManager.NETWORK_PROVIDER)
&& Connectivity.isConnected(this.context)) {
Log.d(LOG_TAG, "Network connected, start listening : " + provider);
this.locationManager.requestLocationUpdates(provider, TIME_INTERVAL, DISTANCE_INTERVAL, this);
} else if (provider.contentEquals(LocationManager.GPS_PROVIDER)
&& Connectivity.isConnectedMobile(this.context)) {
Log.d(LOG_TAG, "Mobile network connected, start listening : " + provider);
this.locationManager.requestLocationUpdates(provider, TIME_INTERVAL, DISTANCE_INTERVAL, this);
} else {
Log.d(LOG_TAG, "Proper network not connected for provider : " + provider);
this.onProviderDisabled(provider);
}
} else {
this.onProviderDisabled(provider);
}
}

public void cancel() {
Log.d(LOG_TAG, "Locating canceled.");
this.locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
Log.d(LOG_TAG, "Location found : " + location.getLatitude() + ", " + location.getLongitude() + (location.hasAccuracy() ? " : +- " + location.getAccuracy() + " meters" : ""));
this.locationManager.removeUpdates(this);
this.callback.onLocationFound(location);
}

@Override
public void onProviderDisabled(String provider) {
Log.d(LOG_TAG, "Provider disabled : " + provider);
if (this.method == Locator.Method.NETWORK_THEN_GPS
&& provider.contentEquals(LocationManager.NETWORK_PROVIDER)) {
// Network provider disabled, try GPS
Log.d(LOG_TAG, "Requesst updates from GPS provider, network provider disabled.");
this.requestUpdates(LocationManager.GPS_PROVIDER);
} else {
this.locationManager.removeUpdates(this);
this.callback.onLocationNotFound();
}
}

@Override
public void onProviderEnabled(String provider) {
Log.d(LOG_TAG, "Provider enabled : " + provider);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(LOG_TAG, "Provided status changed : " + provider + " : status : " + status);
}

public interface Listener {
void onLocationFound(Location location);

void onLocationNotFound();
}

}

最佳答案

您可以找到答案 herehere

另外,注意获取位置不应该在你的 Activity 中处理,尽量让你的 View 尽可能转储

关于android - 如何在 Activity 中使用 Android Location Helper 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677698/

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