gpt4 book ai didi

Java/Android 重写方法

转载 作者:行者123 更新时间:2023-12-01 12:40:35 25 4
gpt4 key购买 nike

我正在练习 Android 实习,我已经能够编写一个类来处理地理定位,以试图找到最佳位置,并且每当调用 onLocationChanged 时我都会不断更新内容。

现在我需要使用我的位置将标记放在 GoogleMaps 上,我显然可以从处理地理定位的类中更改 onLocationChanged 方法,但我希望提取需要在类外执行的操作,因为将来我可能会需要我当前的位置来执行很多不同的事情。

package com.example.soueuls.swipe;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GeoLocation {
private final Context context;
private final LocationManager locationManager;
private Location currentLocation;
private int updateLimit = 0;
private long timeLimit = 0;

public GeoLocation(Context context) {
this.context = context;
this.locationManager = (LocationManager)this.context.getSystemService(Context.LOCATION_SERVICE);
this.currentLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (this.currentLocation == null) {
this.currentLocation = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}

private LocationListener locationListener = new LocationListener() {

@Override
public void onLocationChanged(Location newLocation) {
if (isBetterLocation(GeoLocation.this.currentLocation, newLocation)) {
GeoLocation.this.currentLocation = newLocation;
}

if (--GeoLocation.this.updateLimit == 0) {
GeoLocation.this.stopLocationUpdate();
} else if (System.currentTimeMillis() / 1000 > GeoLocation.this.timeLimit) {
GeoLocation.this.stopLocationUpdate();
}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
};

private boolean isBetterLocation(Location currentLocation, Location newLocation) {
if (currentLocation == null) {
return true;
}

int twoMinutes = 1000 * 60 * 2;

long timeDelta = newLocation.getTime() - currentLocation.getTime();
int accuracyDelta = (int) (newLocation.getAccuracy() - currentLocation.getAccuracy());

boolean isSignificantlyNewer = timeDelta > twoMinutes;
boolean isSignificantlyOlder = timeDelta < -twoMinutes;
boolean isNewer = timeDelta > 0;

if (isSignificantlyNewer) {
return true;
} else if (isSignificantlyOlder) {
return false;
}

boolean isMoreAccurate = accuracyDelta < 0;
boolean isLessAccurate = accuracyDelta > 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
boolean isFromSameProvider = isSameProvider(currentLocation.getProvider(), newLocation.getProvider());

if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
} else {
return false;
}
}

private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}

public void setUpdateLimit(int limit) {
this.updateLimit = limit;
}

public void setTimeLimit(long limit) {
this.timeLimit = System.currentTimeMillis() / 1000 + limit;
}

public void setLocationUpdate(String provider, int minTimeInterval, int minDistance) {
this.locationManager.requestLocationUpdates(provider, minTimeInterval, minDistance, this.locationListener);
}

public void stopLocationUpdate() {
this.locationManager.removeUpdates(this.locationListener);
}

public Location getCurrentLocation() {
return this.currentLocation;
}
}

这是 GeoLocation 类,每次调用 onLocationChanged 时我仍然需要更新 this.currentLocation。

但我希望能够检测到何时调用 onLocationChanged 以在此类之外的 Activity 内执行更新。

我该怎么做?

编辑:在 Activity 的 onResume 方法中,我正在尝试监听

GeolocationListener locationListener = new GeolocationAdapter() {
@Override
public void onLocationChanged(Location newLocation) {
System.out.println("OHOHOH");
displayWeatherInformation(weatherCache, geoLocation.getCurrentLocation());
}
};

最佳答案

使用监听器

将接口(interface)添加到 Geolocation 类

public class GeoLocation {

添加您的听众

   // listeners
private List<LocationListener> listeners = new ArrayList<LocationListener>();
public void addListener(LocationListener l){
listeners.add(l);
}
public void removeListener(LocationListener l){
listeners.remove(l);
}

然后在您放置的private LocationListener locationListener = new LocationListener()

    @Override
public void onLocationChanged(Location newLocation) {
if (isBetterLocation(GeoLocation.this.currentLocation, newLocation)) {
GeoLocation.this.currentLocation = newLocation;
for(LocationListener l:listeners){
l.onLocationChanged(newLocation);
}
}
... the rest of the code

然后任何类(class)都可以注册以接收有关您的地理位置类(class)的更新

编辑:

像这样声明您的 Activity :

public class MyActivity extends Activity implements LocationListener{

@Override
public void onResume(){
geoLocation.addListener(this);
}

@Override
public void onPause(){
geoLocation.removeListener(this);
}

@Override
public void onLocationChanged(Location newLocation) {

}

}

关于Java/Android 重写方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25140986/

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