Search code examples
javaandroidservicegeolocation

How to use Google Location service API to get location updates even in background?


First of all this is not a duplicate of any other question on the site. I've seen location updates using Service and Google API both.

I've tried both, but in case of service, it is not working. But the 1st link's code is working perfectly fine, and gives location updates correctly. But I think It can't get update when the app is in background.

I want that code inside a service, which can continuously get location updates even my app is in background.

But can't figure out how to merge this two codes. If you are asking what I've tried? Than I've just copy the common methods in the service class. But it gives me too many errors :(

If there are alternative available for this please suggest me. I'm new to android.

Thanks in advance!


Solution

  • Try:

    Service.java

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
     import android.location.Location;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.widget.Toast;
    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.common.ConnectionResult;
    import com.google.android.gms.location.LocationServices;
    
    
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {
    
    
    
    private GoogleApiClient mGoogleApiClient;
    
    private LocationRequest mLocationRequest;
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        mGoogleApiClient.connect();
    
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onDestroy() {
    
    
        mGoogleApiClient.disconnect();
        super.onDestroy();
    
    
    
    }
    
    
    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(1000); // Update location every second
    
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
    }
    
    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"GoogleApiClient connection has been suspend",Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"GoogleApiClient connection has failed",Toast.LENGTH_LONG).show();
    }
    

    and MainActivity.java

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Intent i = new Intent(this,GPSService.class);
        startService(i);
    
    
    }