Android LocationListener ...感觉很糟糕:(

时间:2014-11-13 20:57:10

标签: android android-location locationlistener

是否有人能够为LocationListener的onLocationChanged(...)方法找到工作解决方案?看起来很多人都面临着这个问题。 https://code.google.com/p/android/issues/detail?id=57707 即使尝试并尝试了很多方法,我的实施也完全没有被召唤......

public class MyLocationListener implements LocationListener{
    final String _logTag = "Location listener";

    public void onLocationChanged(Location location) {
        String provider = location.getProvider();
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        float accuracy = location.getAccuracy();
        long time = location.getTime();
Log.d("long"," "+lng);
        String logMessage = LogHelper.formatLocationInfo(provider, lat, lng, accuracy, time);
        Log.d(_logTag, "Monitor Location:" + logMessage);
    }

    public void onStatusChanged(String s, int i, Bundle bundle) {
        String statusMessage = LogHelper.translateStatus(i);
      //  Log.d(_logTag, "Monitor Location - Status:" + statusMessage);
        Set<String> keys = bundle.keySet();
        for(String key:keys){
            Log.d(_logTag, "Monitor Location - Bundle Key:" + key);
        }
    }

    public void onProviderEnabled(String s) {
        Log.d(_logTag, "Monitor Location - Provider ENABLED by USER: " + s);
    }

    public void onProviderDisabled(String s) {
        Log.d(_logTag, "Monitor Location - Provider DISABLED by USER: " + s);
    }
}

1 个答案:

答案 0 :(得分:0)

这是我过去使用的,它对我来说非常好用:

public class LocationService 
    extends Service implements GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

    private LocationReceiver locationReceiver = new LocationReceiver();
    public LocationClient locationClient;
    public LocationRequest locationRequest;
    private Location previous;
    String TAG = "LocationService";
    private boolean monitorLocation;
    private boolean googleServicesAvailable = false;
    @Override
    public void onCreate() {
        super.onCreate();
        this.monitorLocation = false;
        //this.locationListener = new LocationListener();
        this.locationRequest = LocationRequest.create();
        this.locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);    // Use high accuracy
        this.locationRequest.setInterval(Singletons.Location.regularIntervalTime);  // Setting the update interval to  5mins
        this.locationRequest.setFastestInterval(Singletons.Location.fastIntervalTime);  // Set the fastest update interval to 1 min
        this.googleServicesAvailable = servicesConnected();
        this.locationClient = new LocationClient(this, this, this);
    }
    public boolean servicesConnected() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode == ConnectionResult.SUCCESS) {
            //Log.d(TAG, "LocationService: Location services available");
            return true;
        } else {
            Log.e(TAG, "Location services not available");
            return false;
        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        if (!this.googleServicesAvailable || this.locationClient.isConnected() || this.monitorLocation) {
            //Log.d(TAG, "LocationService: services already started");
            return START_STICKY;
        }
        setUpLocationClientIfNeeded();
        if (!this.locationClient.isConnected() || !this.locationClient.isConnecting() && !this.monitorLocation) {
            //Log.d(TAG, "LocationService: service is starting now");
            this.monitorLocation = true;
            this.locationClient.connect();
        }
        return Service.START_STICKY;
    }
    public void setUpLocationClientIfNeeded() {
        if (this.locationClient == null) {
            this.locationClient = new LocationClient(this, this, this);
        }
    }
    @Override
    public void onLocationChanged(Location location) {
        if (this.previous == null) {
            this.previous = location;
        }
        if (!this.isSameLocation(location, this.previous)) {
            LocationDataSource datasource = new LocationDataSource(getApplicationContext());
            datasource.open();
            datasource.insertNewLocation(location, this.previous);
            datasource.close();
            this.previous = location;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onConnected(Bundle bundle) {
        this.locationClient.requestLocationUpdates(this.locationRequest, this);
    }
    @Override
    public void onDisconnected() {
        this.monitorLocation = false;
        this.locationClient = null;
    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        this.monitorLocation = false;
        if (connectionResult.hasResolution()) {
            Log.d(TAG, "LocationService: We can resolve the problem?");
            // If no resolution is available, display an error dialog
        } else {
            Log.e(TAG, "LocationService: We cannot resolve the gms location problem");
        }
    }
    @Override
    public void onDestroy() {
        // Turn off the request flag
        this.monitorLocation = false;
        if (this.googleServicesAvailable && this.locationClient != null) {
            //this.locationClient.removeLocationUpdates(this);
            // Destroy the current location client
            this.locationClient = null;
        }
        super.onDestroy();
    }
    public boolean isSameLocation(Location l, Location p) {
        return (p.getLongitude() == l.getLongitude()) && (p.getLatitude() == l.getLatitude());
    }
}