0

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    25 seconds is much too short for `LocationPoller`. For that, you will need to get rid of `LocationPoller`, create a service that will try to run forever and keep GPS on forever to get your location fixes. Users are likely to give you one-star ratings on the Play Store as a result. – CommonsWare May 31 '12 at 16:32
  • Thanks! Glad i am only testing :P i am noob at adnroid development. Thanks again!. – user1356630 May 31 '12 at 16:45
  • Sry can you tell the recommended period?. Thaks in advance Pamela – user1356630 May 31 '12 at 16:48
  • The "recommended period" has little to do with `LocationPoller` and everything to do with whatever it is your app is trying to achieve. As your boss, or your prospective users, or somebody how frequently you need to update the location data, taking into account the impact on battery life. Then if the period you choose is, say, under 5 minutes, `LocationPoller` is probably not a good choice. – CommonsWare May 31 '12 at 16:50
  • @CommonsWare If the period is under 5 minutes, So what is good choice, If LocationPoller is not good choice? Service? Do you have any sample? – Dr.jacky Jul 14 '14 at 12:40
  • @Mr.Hyde: Well, I have discontinued `LocationPoller`, and I have not attempted to implement anything along the same lines. However, if the Play Services Framework is OK for you, I would consider `LocationClient` and have it execute a `PendingIntent` when it gets fixes to run your code. – CommonsWare Jul 14 '14 at 22:43
  • @CommonsWare What about Alex Birkett's extended library? Is that support time period less than a minute? – Dr.jacky Jul 15 '14 at 11:36
  • 1
    @Mr.Hyde: You would have to ask Mr. Birkett. – CommonsWare Jul 15 '14 at 11:40
  • @CommonsWare Sorry but how can i mention him? – Dr.jacky Jul 15 '14 at 11:49
  • @Mr.Hyde: I have no idea if and how he supports his fork. I have not looked at it in a couple of years. – CommonsWare Jul 15 '14 at 11:52
  • @CommonsWare He said "If you have questions regarding the use of this code, please post a question on StackOverflow tagged with commonsware and android." :D. Thanks any way. – Dr.jacky Jul 15 '14 at 14:21

0 Answers0