What I'm trying to do is get a c2dm message, start a service that requests a location for "x" time, and then passes that location to our server. The c2dm message starts the service correctly and the GPS location is turned on, but it is never updated. It just sits there for a given time (currently 12 seconds) in the stream and does nothing. I use the same code somewhere else in my application (and not as a service), and it works great. What am I doing wrong?
This starts the service when a c2dm message is received.
context.startService(new Intent(context, ServicePingLocation.class));
This is the code for the service itself. All that has ever been called is "onCreate" and "onStart".
public class ServicePingLocation extends Service implements LocationListener { private final String DEBUG_TAG = "[GPS Ping]"; private boolean xmlSuccessful = false; private boolean locationTimeExpired = false; private LocationManager lm; private double latitude; private double longitude; private double accuracy; @Override public void onLocationChanged(Location location) { Log.d(DEBUG_TAG, "onLocationChanged"); latitude = location.getLatitude(); longitude = location.getLongitude(); accuracy = location.getAccuracy(); } @Override public void onProviderDisabled(String provider) { Log.d(DEBUG_TAG, "onProviderDisabled"); Toast.makeText( getApplicationContext(), "Attempted to ping your location, and GPS was disabled.", Toast.LENGTH_LONG).show(); } @Override public void onProviderEnabled(String provider) { Log.d(DEBUG_TAG, "onProviderEnabled"); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10f, this); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d(DEBUG_TAG, "onStatusChanged"); } @Override public void onCreate() { Log.d(DEBUG_TAG, "onCreate"); } @Override public void onDestroy() { Log.d(DEBUG_TAG, "onDestroy"); } @Override public IBinder onBind(Intent intent) { Log.d(DEBUG_TAG, "onBind"); return null; } @Override public void onStart(Intent intent, int startid) { Log.d(DEBUG_TAG, "onStart"); lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10f, this); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this); Log.d(DEBUG_TAG, lm.toString()); new SubmitLocationTask(ServicePingLocation.this).execute(); } private void locationTimer() { new Handler().postDelayed(new Runnable() {
}
[edit] The problem that I encountered was fixed. The code really worked. I added a network provider, adjusted the onDestroy () method to stop the service, and changed the time used to capture the GPS signal.
Thanks for the tip, CommonsWare