If you want to listen to frequent location updates in the background (for example, every second), you must run your code inside the Service :
http://developer.android.com/reference/android/app/Service.html
Activity can be completed by the Android platform at any time when they are not in the foreground.
When using the Service, I would recommend that the Service implement the LocationListener directly, and not Thread inside the Service. For example, use:
public class LocListener extends Service implements com.google.android.gms.location.LocationListener, ...{
I used this project to implement a LocationListener directly in the Service with a LocationClient and a smooth access provider in my application https://developer.android.com/reference/com/google/android/gms/location/LocationClient.html#requestLocationUpdates(com.google .android.gms.location.LocationRequest,% 20android.app.PendingIntent)
From the above Android document:
This method is suitable for use in the background, more specifically for receiving location updates, even when the application was killed by the system. To do this, use PendingIntent for the running service. For cases of using the foreground, it is recommended to use the version of the LocationListener method, see RequestLocationUpdates (LocationRequest, LocationListener).
All previous LocationRequests registered on this PendingIntent will be replaced.
Location updates are sent with the key KEY_LOCATION_CHANGED and the location value in the intent.
For a more detailed description of using PendingIntents see the Activity Recognition example to receive updates while running in the background:
https://developer.android.com/training/location/activity-recognition.html
Modified excerpts from this documentation are given below, modified by me to be specific to location updates.
First declare the intention:
public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener { ... ... private PendingIntent mLocationPendingIntent;
Request updates as you are now, but this time pass in anticipation of an intention:
Intent intent = new Intent( mContext, LocationIntentService.class); ... //Set up LocationRequest with desired parameter here ... mLocationPendingIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); mLocationClient.requestLocationUpdates(mLocationRequest, callbackIntent);
Location Updates for Mailboxes
To handle the intent that location services send for each update interval, define the IntentService and its required onHandleIntent () method. Location services send ... updates as Intent objects using the PendingIntent that you specified when calling requestLocationUpdates (). Since you provided an explicit intent for the PendingIntent, the only component that receives the intent is the IntentService that you define.
Define the class and the required onHandleIntent () method:
public class LocationIntentService extends IntentService { ... @Override protected void onHandleIntent(Intent intent) { Bundle b = intent.getExtras(); Location loc = (Location) b.get(LocationClient.KEY_LOCATION_CHANGED); Log.d(TAG, "Updated location: " + loc.toString()); } ... }
IMPORTANT - in order to be as efficient as possible, your code in onHandleIntent() must return as soon as possible to allow the IntentService to exit. From the IntentService docs:
http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent)
This method is called on a workflow with a request for processing. Only one Intent is processed at a time, but processing occurs in a workflow that runs independently of other application logic. Thus, if this code takes a lot of time, it will delay other requests to the same IntentService, but it will not delay anything. When all requests are processed, the IntentService stops, so you should not call stopSelf ().
My understanding of the design of IntentService is that you can create threads inside onHandleIntent() to avoid blocking other location updates through platform calls on onHandleIntent() , just keep in mind that the Service will continue to work until all running threads to stop.