I'm building an application for Android to track the user GPS positioning during a ride (by sending events with positioning information to a server), but I've got two issues so far. Since the user could suspend the application in order to use other applications during the ride, application would stop to send events. To turn around this issue, I've implemented a background service that keeps running even when the user switches to another application, and consequently keeps sending events to the server. However it seems that somehow that service is suspended whenever the phone turns off its screen (go into sleeping mode?). The second issue is about getting GPS positioning data from the phone. I've put the necessary permissions for the application to get data from GPS receiver, and actually, sometimes, the positiong is retrieved correctly (at the same moments when there are other applications using GPS as well). When there are no other applications using GPS, the positioning retrieved is the tuple (0.0, 0.0) for longitude and latitude. It's unclear to me why these two issues are taking place. Why the background service is getting suspended whenever the screens get turned off, and why GPS returns the correct positioning only when there are other applications requesting GPS use?
I faced off with same problem(first issue). I resolve it via using looper inside of service. For second issue we must see some your codeto help you. Also I'll show my code for search location maybe it will help. I used GoogleApiClient for it https://developer.android.com/training/location/retrieve-current.html
public class LocationService extends Service implements com.google.android.gms.location.LocationListener {
private Thread mTriggerService;
private Handler mLooperHandler;
...
private void addLocationListener(){
// Start thread which listen location
mTriggerService = new Thread(new Runnable(){
public void run(){
try{
Looper.prepare();//Initialise the current thread as a looper.
mLooperHandler = new Handler();
initLocationManager();
Looper.loop();
}catch(Exception ex){
ex.printStackTrace();
}
}
}, "LocationThread");
mTriggerService.start();
}
/**
* Strart listening location
*/
public void initLocationManager(Context context) {
...
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(mInterval);
//This controls the fastest rate at which your application will receive location
//updates, which might be faster than setInterval(long) in some situations
//(for example, if other applications are triggering location updates).(from doc)
mLocationRequest.setFastestInterval(mFastestInterval);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected");
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "onConnectionSuspended");
}
})
.addOnConnectionFailedListener(mOnConnectionFailedListener)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
//stop listening location
private void stopLocationListener() {
if(mLocationHelper != null){
mLooperHandler.getLooper().quit();
if(mGoogleApiClient != null && mLocationListener != null
&& mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
}
}
}