Skip to main content

Locationlistener on Background Services

Before creating this service if you have requesting location update on the other part of your app, make sure you call removeLocationUpdate(). So the service can get the right location update.
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, context);
public class LocationTrackingService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener{

    protected GoogleApiClient googleApiClient;

    @Override
    public void onCreate() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if(googleApiClient != null && !googleApiClient.isConnected())
            googleApiClient.connect();
        else if(googleApiClient == null) {
            createGoogleApiClient();
        }

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
        googleApiClient.disconnect();
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("LOCATION CHANGED", "onLocationChanged: "+ location.toString());
    }

    @Override
    public void onConnected(Bundle bundle) {

        LocationRequest locRequest = new LocationRequest();
        locRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locRequest.setInterval(60000*15); // update location every 15 minutes
        locRequest.setFastestInterval(60000*5); // set fastest for 5 minutes
        locRequest.setSmallestDisplacement(10);
        startLocationUpdate(locRequest);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    private void startLocationUpdate(LocationRequest locRequest){
        // checkpermission for location access on Android 6.0
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    googleApiClient, locRequest, this);
        }
    }

    private createGoogleApiClient(){
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        googleApiClient.connect();
    }
}

Comments

Popular posts from this blog

Automatic Refresh API Access Token with Retrofit and Okhttp Authenticator

This tutorial will be tell how to use Okhttp Authenticator with Retrofit. This authenticator method is usefull when we having a case like : Re-authorization without a user interaction (login) Requesting new API access token with refresh token This tutorial will cover the second case ( access token  dan  refresh token ) which is commonly use. The case will have scenario like this : Normal API request. When token or API key expired, API will return 401. Okhttp Authenticator will intercept the process. Okhttp Authenticator will requesting new token with refresh token. New token receive and API request will be reloaded automatically. So, first create ApiAdapter class which is a class that contain retrofit singleton. In this class add method getAdapter(). public class ApiClient {     public static Retrofit getAdapter() {         if (retrofit==null) {            final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();            httpClient.addInterceptor(new Intercepto

How to add host ip address on Android Virtual Device (AVD) or Genymotion using Android Studio

Select  terminal Enter this script AVD adb shell echo '10.0.2.2 hostname' /system/etc/hosts Genymotion adb shell echo '10.0.3.2 hostname' /system/etc/hosts Enter this script to check if the host successfully added adb shell cat /system/etc/hosts Then terminal will print the hosts file list  Enter http://hostname from emulator browser to check if the host can be accessed