locpoll to get a location in a period (25 seconds) and it seems that sometimes it stops sending location updates or the receiver stops receiving. Here is my code. Is anything wrong with it?.
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle b = intent.getExtras();
Location loc = (Location) b.get(LocationPoller.EXTRA_LOCATION);
String msg;
if (loc == null) {
loc = (Location) b.get(LocationPoller.EXTRA_LASTKNOWN);
if (loc == null) {
msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
cPosicion.latitud = 0;
cPosicion.longitud = 0;
cPosicion.timeStamp = System.currentTimeMillis();
} else {
msg = "TIMEOUT, lastKnown=" + loc.toString();
Log.e("e_timeout", "timeout");
cPosicion.latitud = 0;
cPosicion.longitud = 0;
cPosicion.timeStamp = System.currentTimeMillis();
}
} else {
msg = loc.toString();
cPosicion.latitud = loc.getLatitude();
cPosicion.longitud = loc.getLongitude();
cPosicion.timeStamp = System.currentTimeMillis();
}
if (msg == null) {
msg = "Invalid broadcast received!";
}
} catch (Exception e) {
Log.e(getClass().getName(), "Exception", e);
}
}
}
this is how I start the gps
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
} else {
startGPS();
}
}
// .....
private void startGPS() {
mgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(this,
LocationReceiver.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER, LocationManager.GPS_PROVIDER);
pi = PendingIntent.getBroadcast(this, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), PERIOD, pi);
}
this is how I close the activity
private void stopGPS() {
mgr.cancel(pi);
}
Thanks in advance Pamela