1

我有这个类用于使用 GooglePlayServices(FusedLocationAPI) 获取位置:

GetLocation.class:

package com.entu.bocterapp;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * Created by Soulstorm on 2/4/2015.
 */
public class GetLocation implements LocationListener,     GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private static final long ONE_MIN = 1000 * 60;
private static final long TWO_MIN = ONE_MIN * 2;
private static final long FIVE_MIN = ONE_MIN * 5;
private static final long POLLING_FREQ = 1000 * 30;
private static final long FASTEST_UPDATE_FREQ = 1000 * 5;
private static final float MIN_ACCURACY = 25.0f;
private static final float MIN_LAST_READ_ACCURACY = 500.0f;

Context mContext;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;

LocationResultReceived locationResultReceived;

public GetLocation(Context mContext, LocationResultReceived locationResultReceived) {

    this.mContext = mContext;
    this.locationResultReceived = locationResultReceived;

    buildGoogleApiClient();
    mGoogleApiClient.connect();
    createLocationRequest();
}

public GetLocation() {}

@Override
public void onConnected(Bundle bundle) {
    // Get first reading. Get additional location updates if necessary
    if (servicesAvailable()) {
        // Get best last location measurement meeting criteria
        bestLastKnownLocation();
    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onLocationChanged(Location location) {

    float bestAccuracy = Float.MAX_VALUE;
    if (location != null) {
        float accuracy = location.getAccuracy();

        if (accuracy < bestAccuracy) {
            locationResultReceived.locationReceived(location);
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    } else {
        locationResultReceived.locationReceived(location);
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(mContext)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

protected void createLocationRequest() {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(POLLING_FREQ);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
}

private void bestLastKnownLocation() {
    // Get the best most recent location currently available
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

private boolean servicesAvailable() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);

    if (ConnectionResult.SUCCESS == resultCode) {
        return true;
    } else {
        Toast.makeText(mContext, "Google play services connection failed! Won't be able to get location.", Toast.LENGTH_LONG).show();
        return false;
    }
}

public void removeLocationUpdates () {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

public static abstract class LocationResultReceived {
    public abstract void locationReceived(Location location);
}
}

我在 2 个不同的活动中使用它,例如:

 GetLocation.LocationResultReceived locationResult = new GetLocation.LocationResultReceived() {
            @Override
            public void locationReceived(Location location) {
                currentLatitude = location.getLatitude();
                currentLongitude = location.getLongitude();
                lastLocation.setLatitude(location.getLatitude());
                lastLocation.setLongitude(location.getLongitude());
                ParseUser.getCurrentUser().put("lastLocation", lastLocation);
                ParseInstallation.getCurrentInstallation().put("lastLocation", lastLocation);
                ParseInstallation.getCurrentInstallation().saveInBackground();
            }
        };
        GetLocation getLocation = new GetLocation(this, locationResult);

问题是我想在活动暂停或销毁时取消位置更新,但是当我打电话时:

public void removeLocationUpdates () {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

从另一个类,它给了 NullPointer 并且它崩溃了。

如果从另一个类调用 GetLocation,有没有办法取消位置更新?

干杯!

4

0 回答 0