FusedLocationAPI无法通过AlarmManager

时间:2018-04-04 06:51:03

标签: android alarmmanager fusedlocationproviderapi fusedlocationproviderclient

我创建了一个简单的类来使用Google的FusedLocationProviderClient 获取更新的位置。即:

public class LocationProvider extends LocationCallback {...
  private static FusedLocationProviderClient mFusedLocationClient;
  private LocationRequest mLocationRequest;

    if(mFusedLocationClient == null) {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                locationCallback,
                null /* Looper */);
    }..}

现在,我使用AlarmManager 在后台更新位置。但是,我无法移除位置更新

public void stopLocationRequest(){
    if(mFusedLocationClient != null){
        mFusedLocationClient.removeLocationUpdates(locationCallback);
        mFusedLocationClient = null;
        mLocationRequest = null;
    }
}
private LocationCallback locationCallback = new LocationCallback(){
    @Override
    public void onLocationResult(LocationResult locationResult) {
        super.onLocationResult(locationResult);
        Location location = locationResult.getLastLocation();
        //Code of Generate Notification
    }};

现在,我在BroadcastReceiver上调用 stopLocationRequest 函数,它在Notification.Builder中注册为addAction()。但是我收到错误:

    A/UnregisterListenerTask: Received call to unregister a listener without a matching registration call.
java.lang.Exception
    at com.google.android.gms.internal.zzbar.zzb(Unknown Source)
    at com.google.android.gms.internal.zzban.zza(Unknown Source)
    at com.google.android.gms.internal.zzbdd.zzb(Unknown Source)
    at com.google.android.gms.internal.zzbdd.zza(Unknown Source)
    at com.google.android.gms.internal.zzbdb.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:135)
    at android.os.HandlerThread.run(HandlerThread.java:61)

而且,我仍然会获得更新的位置。它不会删除位置更新。请给我任何关于这个问题的建议。

0 个答案:

没有答案