无法从FusedLocationApi中删除更新

时间:2015-01-22 20:10:58

标签: java android google-api

我正在尝试获取位置更新并将其发送到待处理的意图,我已经完成了这项工作。然而有一个按钮,当它按下我希望它停止获取位置更新时,这是我的代码:

MainActivity

public class MainActivity extends Activity implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {   
GoogleApiClient mGoogleClient;
PendingIntent pendingIntent;
LocationRequest locationRequest;
LocationServices locationServices;
...
protected void onStart() {
    super.onStart();

    mGoogleClient.connect();
}

@Override
protected void onStop() {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleClient, pendingIntent);

    super.onStop();
}

@Override
public void onLocationChanged(Location location) {
    // this callback is invoked when location updates
}

@Override
public void onConnected(Bundle connectionHint) {

    try {
        locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setFastestInterval(30000)
                .setInterval(60000);
        pendingIntent = PendingIntent.getService(this, 0,
                new Intent(this, MyLocationHandler.class),
                PendingIntent.FLAG_UPDATE_CURRENT);
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleClient, locationRequest, pendingIntent);
        //Toast.makeText(this, "Adding Toast", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        ...
    }

}

@Override
public void onConnectionSuspended(int cause) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    // this callback will be invoked when the connection attempt fails

    if (connectionResult.hasResolution()) {
        // Google Play services can fix the issue
        // e.g. the user needs to enable it, updates to latest version
        // or the user needs to grant permissions to it
        try {
            connectionResult.startResolutionForResult(this, 0);
        } catch (IntentSender.SendIntentException e) {
            // it happens if the resolution intent has been canceled,
            // or is no longer able to execute the request
        }
    } else {
        // Google Play services has no idea how to fix the issue
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGoogleClient = new GoogleApiClient.Builder(this, this, this)
                .addApi(LocationServices.API)
                .build();
         TrackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    bTrack = !bTrack;
                    CheckTrack();
                }
            }
        });

}
...
public void CheckTrack() {
    if (a == null) {
        a = tracker.getLocation();

    }
    if (bTrack) {

            mGoogleClient.connect();

    } else {

            DisconnectHandlerCheck.postDelayed(DisconnectRunnableCheck, 500);            }
    }
}
Handler DisconnectHandlerCheck = new Handler();
Runnable DisconnectRunnableCheck = new Runnable() {

@Override
public void run() {
    try {

        onStop();

    } catch (Exception e) {
        ...
    }
}
};
}

由于某种原因,即使我断开了Google客户端并删除了更新,代码仍继续调用pendingIntent。

为什么想法?

2 个答案:

答案 0 :(得分:0)

尝试使用: mGoogleClient.disconnect(); 在您的onStop()/方法版本中。

答案 1 :(得分:0)

断开mGoogleClient后,使用pendingIntent.cancel()取消待处理的意图。它对我有用。