Java 删除位置更新问题。位置类与使用位置不同,无法删除位置更新(FusedLocationAPI)
我使用GooglePlayServicesUsedLocationAPI来获取位置: GetLocation.class:Java 删除位置更新问题。位置类与使用位置不同,无法删除位置更新(FusedLocationAPI),java,android,location,fusedlocationproviderapi,Java,Android,Location,Fusedlocationproviderapi,我使用GooglePlayServicesUsedLocationAPI来获取位置: 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.
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);
}
}
问题是,当活动暂停或销毁时,我想取消位置更新,但当我调用:
public void removeLocationUpdates () {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
从另一个类中,它给出null指针,然后崩溃
如果从另一个类调用GetLocation,是否有方法取消位置更新
干杯 在删除之前,请检查mgoogleapclient!=null最好在服务中执行此操作,并调用intent.setActionyour.package.stop.location.update,并且必须使用mGoogleApiClient.isConnected检查它是否已连接
public void removeLocationUpdates () {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}