Skip to content Skip to sidebar Skip to footer

Android Location Service Didn't Work In Background

I am working on a Location-App which should begin to track some statistic data based on longitude and latitude when the user presses a Button. All this stuff works very well but wh

Solution 1:

Use fuse location service and save updated location every time

publicclassLocationNotifyServiceextendsServiceimplementsLocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    publicstaticLocation mCurrentLocation;

    .............................

    @OverridepublicvoidonCreate() {

        //show error dialog if GoolglePlayServices not availableif (isGooglePlayServicesAvailable()) {
            mLocationRequest = newLocationRequest();
        mLocationRequest.setInterval(BACKGROUND_INTERVAL);  
        mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(10.0f);  /* min dist for location change, here it is 10 meter */
            mGoogleApiClient = newGoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }

    @Overridepublic int onStartCommand(Intent intent, int flags, int startId) {
         returnsuper.onStartCommand(intent, flags, startId);
    }

    //Check Google play is available or notprivatebooleanisGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        returnConnectionResult.SUCCESS == status;
    }


    @OverridepublicvoidonConnected(Bundle bundle) {
        startLocationUpdates();
    }

    protectedvoidstartLocationUpdates() {
        try {
             PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
        } catch (IllegalStateException e) {}
    }

    @OverridepublicvoidonLocationChanged(Location location) {

        //Save your location
    }
    ............................
}

you can get current location onLocationChanged()

For more details check http://javapapers.com/android/android-location-fused-provider/ or follow official guide https://developer.android.com/training/location/index.html

Solution 2:

Here is application that does something like you want:

https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/CosyDVR.java

Main Activity starts on BackgroundVideoRecorder service.

Intent intent = newIntent(/*CosyDVR.this*/getApplicationContext(), BackgroundVideoRecorder.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

look at mConnection onServiceConnected,onServiceDisconnected.

Here is BackgroundVideoRecorder class:

https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/BackgroundVideoRecorder.java

It implements LocationListener. So has onLocationChanged, onProviderDisabled, onProviderEnabled, onStatusChanged.

Post a Comment for "Android Location Service Didn't Work In Background"