0

My Activity is like this:

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
    }

}

The code works just fine in the foreground - what I want to do is to make sure that location is constantly tracked (even when the app is in the background or the phone is in sleep mode) and launch the appropriate function in my Activity.

Marinos K
  • 1,779
  • 16
  • 39
  • for background stuff using a service helps. whatever the work you are doing in the foreground activity, perform them in a service to keep it working in background. – Simo Apr 22 '17 at 15:01
  • if just move all of these functions inside a service will it be ok? – Marinos K Apr 22 '17 at 15:02
  • yes. some modification might be needed according to your work. but it will work. – Simo Apr 22 '17 at 15:03
  • Have look at this answer [this answer](http://stackoverflow.com/a/28535885/6904440) – Simo Apr 22 '17 at 15:07

0 Answers0