Java/Android覆盖方法

Java/Android覆盖方法,java,android,methods,overriding,Java,Android,Methods,Overriding,我正在练习Android进行实习,我已经能够编写一个类来为我处理地理位置,试图找到最佳位置,并且每当调用onLocationChanged时,我都会不断更新内容 现在我需要使用我的位置在GoogleMaps上放置标记,我可以从处理地理位置的类中更改onLocationChanged方法,但我希望提取我需要在类外执行的操作,因为将来我可能需要我的当前位置来执行许多不同的操作 package com.example.soueuls.swipe; import android.content.Con

我正在练习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;
    }
}

您可以使用观察者模式,活动在onResume()中与GeoLocation类一起注册自身以接收更新,并在onPause()中注销自身

网上有很多学习观察者模式的材料-

使用监听器

   // listeners
   private List<LocationListener> listeners = new ArrayList<LocationListener>();
   public void addListener(LocationListener l){
      listeners.add(l);
   }
   public void removeListener(LocationListener l){
      listeners.remove(l);
   }
将接口添加到地理位置类

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);
   }
然后,任何类都可以注册以接收地理位置类的更新

public class GeoLocation {
编辑:

像这样声明您的活动:

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) {

    }

}

您应该实现listener模式。listener是肯定的!我将发布一个例子。我在注册地理定位课程时遇到了一些困难。我将用我正在做的事情更新我的初始帖子。你不应该创建一个
新的
,而应该像这样声明活动:
公共类MyActivity extends activity implements LocationListener
,然后编写方法
onLocationChanged(Location)
在活动和onResume/onPause中,您必须调用
geoLocation.addListener(此)
地理定位。removeListener(此)请查看我的编辑。不要问你是如何获得对
地理位置的引用的,这是一个不同的问题,答案是
Singleton