I'm trying to write a simple Android Service that runs in background and receives location updates from LocationClient (Google Map API Android V2). The problem is that when the screen go off, my Service doesn't receives anymore location updates. I tried to check if the service was active, even with screen off, and it does (I have a TimerTask scheduling a log). When screen is on I can receive location updates, but when screen goes off I see only TimerTask's logs and I don't receive any location update. Waking up screen turns on location updates again. How can this be solved?
Here is my simple service:
public class LocationService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener{
private static final String TAG = LocationService.class.getSimpleName();
private LocationClient mLocationClient;
private Timer timer;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5*1000) // 5 seconds
.setFastestInterval(3*1000) // 3 seconds
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
public void onCreate() {
Log.d(TAG, "Creating..");
mLocationClient = new LocationClient(this, this, this);
timer = new Timer();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting..");
if(!mLocationClient.isConnected() || !mLocationClient.isConnecting()) {
Log.d(TAG, "Connecting location client..");
mLocationClient.connect();
}
timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
Log.d(TAG, "TimerTask executing");
}}, 0, 3*1000);
return START_STICKY;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d(TAG, "Connection failed..");
stopSelf();
}
@Override
public void onConnected(Bundle bundle) {
System.out.println("Connected ...");
mLocationClient.requestLocationUpdates(REQUEST, this);
}
@Override
public void onDisconnected() {
Log.d(TAG, "Disconnected..");
stopSelf();
}
@Override
public IBinder onBind(Intent arg0) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Received location update");
}
@Override
public void onDestroy() {
Log.d(TAG, "Destroying..");
timer.cancel();
mLocationClient.removeLocationUpdates(this);
}
}