我的活动是这样的:

public class MainMap extends FragmentActivity implements OnMarkerClickListener, OnMapReadyCallback,
        ConnectionCallbacks, OnConnectionFailedListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {

    private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
    protected GoogleApiClient mGoogleApiClient;
    protected Location mCurrentLocation;
    protected LocationRequest mLocationRequest;
    // ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(NativeLib.get_location_update_interval_in_mil());
        mLocationRequest.setFastestInterval(NativeLib.get_location_fastest_update_interval_in_mil());
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    protected void onStart() {
        if (debug_mode) { Log.i(TAG, "onStart");}
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        if (debug_mode) { Log.i(TAG, "onStop");}
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        if (debug_mode) { Log.i(TAG, "onConnected");}
        // permissions
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            request_fine_location_permission();
        }
        // location
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        // CALL either do_background or do_foreground
    }

    private void do_background() {
        // DO WORK
    }

    private void do_foreground() {
        // DO WORK
    }

}

代码在前台工作得很好 - 我想要做的是确保持续跟踪位置(即使应用程序在后台或手机处于睡眠模式)并在我的Activity中启动相应的功能 .