Android Location service did not work in the background

I am working on a Location-App that should start tracking some statistics based on longitude and latitude when the user clicks Button . All this works very well, but when it comes to locking the screen or installing the application in the background, the service no longer works!

I read a lot about background services and broadcast receivers, but I don’t know how to implement the Google API Location receiver in the service and where to implement this class in MainActivity . Can someone tell me an example with a short code, how to implement such a service or a link where this is explained?

+3
android background service location
Jan 21 '16 at 8:25
source share
2 answers

Use the fuse location service and keep updated location every time

 public class LocationNotifyService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; public static Location mCurrentLocation; ............................. @Override public void onCreate() { //show error dialog if GoolglePlayServices not available if (isGooglePlayServicesAvailable()) { mLocationRequest = new LocationRequest(); 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 = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } //Check Google play is available or not private boolean isGooglePlayServicesAvailable() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); return ConnectionResult.SUCCESS == status; } @Override public void onConnected(Bundle bundle) { startLocationUpdates(); } protected void startLocationUpdates() { try { PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); } catch (IllegalStateException e) {} } @Override public void onLocationChanged(Location location) { //Save your location } ............................ } 

you can get the current location onLocationChanged()

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

+3
Jan 21 '16 at 9:27
source share

Here is an application that does something like what you need:

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

The main work begins with the BackgroundVideoRecorder service.

 Intent intent = new Intent(/*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 the BackgroundVideoRecorder class:

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

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

0
Jan 21 '16 at 9:17
source share



All Articles