Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GPS timeout

Tags:

android

gps

Edit: I'm rewriting this question because I apparently wasn't clear.

Sometimes the GPS service on Android phones takes a long time to get a fix. Sometimes it's fast, sometimes it takes hours. I know and accept this.

I have an application that does many things. One of the things it must do is allow the user to click a button to send their current coordinates to a server. What I need are the coordinates of the phone when the user clicks the button or within a reasonably short time thereafter.

Because I know that getting a GPS fix is not instant and I know that it could take minutes or hours (during which the user has moved a great distance), I need to code a timeout for this feature. For this feature, it is simply not acceptable to upload the GPS location of the user three minutes (for example) after they clicked the button. It's fine if it takes 45 seconds, not okay if it takes 75 seconds. It's fine to give the user an error notification if the feature failed to get a location fast enough.

I need a feature to 'get the GPS location and send it to the server, unless it takes more than one minute'.

My original code is below. I have changed some things since posting it. I have added a Timer in the onStartCommand() method. I start a TimerTask that after 60 seconds will call my stop() method. At the beginning of the onLocationChanged() method, I cancel the TimerTask.

My question is: Is the Timer scheme a good way of implementing this timeout? Is there a better way?

Original question:

I'm writing an Android application that, among other things, needs to send the current GPS coordinates to a server when the user tells it to. From a context menu, I run the service below. The service is a LocationListener and requests updates from the LocationManager. When it gets a location (onLocationChanged()), it removes itself as a listener and sends the coordinates off to the server. All of this is working.

However, if GPS coordinates are not quickly available, my service just keeps running until it gets some. It holds up the UI with a progress dialog, which is annoying. Worse, if the user has moved since starting the service, the first GPS coordinates might be wrong and the app will send bad data to the server.

I need a timeout on the service. Is there a good way to do that? I'm not very experienced with threads. I think I can run a Runnable in the onStartCommand() method that will somehow count down 30 seconds and then, if there is no GPS result yet, call my service's stop() method. Does that sound like the best way to do this?

Alternatively, is it possible to tell if the GPS cannot get a fix? How would I go about doing that?

Edit: To further clarify, I'm looking for the best way to "give up" on getting a Location after some amount of time.

public class AddCurrentLocation extends Service implements LocationListener {

    Application app;
    LocationManager mLocManager;
    ProgressDialog mDialog;

    @Override
    public int onStartCommand(Intent intent, int arg0, int arg1) {
        app = getApplication();

        // show progress dialog
        if (app.getScreen() != null) {
            mDialog = ProgressDialog.show(app.getScreen(), "", "Adding Location. Please wait...", true);
        }

        // find GPS service and start listening
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String bestProvider = mLocManager.getBestProvider(criteria, true);
        mLocManager.requestLocationUpdates(bestProvider, 2000, 0, this);

        return START_NOT_STICKY;
    }

    private void stop() {
        mLocManager.removeUpdates(this);
        if (mDialog != null) {
            mDialog.dismiss();
        }
        stopSelf();
    }

    @Override
    public void onLocationChanged(Location location) {
        // done with GPS stop listening
        mLocManager.removeUpdates(this);

        sendLocation(location); // method to send info to server
        stop();
    }

    // other required methods and sendLocation() ...

}
like image 732
Scott Saunders Avatar asked Jun 07 '10 19:06

Scott Saunders


People also ask

How do I fix my GPS on my Android phone?

Navigate to Settings > Location > and make sure Location is ON. Navigate to Settings > Loction > Sources Mode and tap High Accuracy. NOTE: GPS accuracy varies depending on the number of visible GPS satellites. Locating all visible satellites can take several minutes, with accuracy gradually increasing over time.

How do I turn on GPS automatically on Android?

First we select YES from dialog asking 'You need to turn on GPS to continue' or something like that (typical traditional alert dialog). Then it moves to the common GPS screen, showing list of other apps accessing GPS. However, next we toggle the location switch.


2 Answers

That's not really how it works. It will consistently take that long in most situations to get a GPS fix. But from that point on each update (every 2 sec in your code)will be the person's current position. And the first fix you get will be the person's current position, so the data will not be "out of date".

Another thing. If you are running this code in a service you shouldn't block the UI with a progress dialog and definitely not from the Service. That is a memory leak waiting to happen. You should only show progress if it is something that might take 5 sec at the most and is running in a thread in the Activity. Another option is to show the progress dialog in the title bar, and still let the user interact with the app (which is why you use a service anyway). Showing progresses for a long period of time really isn't that User Friendly. Especially if they somehow change orientation (maybe on accident) and then your app crashes because of the service handle to the dialog and they have to start over.

Take a look at the Google I/O 2010 app to see a great example of how an activity should work with a service. It uses a service to pull back data, and shows a progress in the title while the service is doing some work. And still lets you do other things in the app.

like image 106
Robby Pond Avatar answered Nov 11 '22 16:11

Robby Pond


Scott, there are many factors that affect how long a first fix can take - or even whether a fix can be achieved, the most common being physical obstacles between the device and satellites (sucha s buildings, canyon walls, etc).

You can't control how long it takes for the GPS engine to deliver a fix, but you can tell how its doing, including time of first fix:

locationManager.addGpsStatusListener(gpsListener);

    // this reports on the status of the GPS engine, but does not enable additional controls 
    private static final GpsStatus.Listener gpsListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            GpsStatus gpsStatus = locationManager.getGpsStatus(null);
            switch (event) {
                case GpsStatus.GPS_EVENT_STARTED: 
                    Log.i(TAG, "onGpsStatusChanged(): GPS started");
                    break;
                case GpsStatus.GPS_EVENT_FIRST_FIX: 
                    Log.i(TAG, "onGpsStatusChanged(): time to first fix in ms = " + gpsStatus.getTimeToFirstFix());
                    break;
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
                    // int maxSatellites = gpsStatus.getMaxSatellites();    // appears fixed at 255
                    // if (H.DEBUG) Log.d(TAG, "onGpsStatusChanged(): max sats = " + maxSatellites);
                    if (H.VERBOSE) Log.d(TAG, "onGpsStatusChanged(): ##,used,s/n,az,el");
                    Iterable<GpsSatellite>satellites = gpsStatus.getSatellites();
                    Iterator<GpsSatellite>satI = satellites.iterator();
                    while (satI.hasNext()) {
                        GpsSatellite satellite = satI.next();
                        if (H.VERBOSE) Log.d(TAG, "onGpsStatusChanged(): " + satellite.getPrn() + "," + satellite.usedInFix() + "," + satellite.getSnr() + "," + satellite.getAzimuth() + "," + satellite.getElevation()); 
                        // http://en.wikipedia.org/wiki/Global_Positioning_System: the almanac consists of coarse orbit and status information for each satellite
                        // http://en.wikipedia.org/wiki/Ephemeris: the positions of astronomical objects in the sky at a given time
                        // + "," + satellite.hasAlmanac() + "," + satellite.hasEphemeris());
                    }
                    break;
                case GpsStatus.GPS_EVENT_STOPPED: 
                    Log.i(TAG, "onGpsStatusChanged(): GPS stopped");
                    break;
            }       
        }
    };

Events will be generated as the engine attempts to listen to available satellites. On a recent test of this with light obstacles I found it took 22.4 seconds to get an initial fix, during which 24 SATELLITE_STATUS events reporting the gradual access of 8 satellites before sufficiently clean signals were received to achieve the fix. Here is the last event:

06-08 23:23:25.147,D,GPS,22427,"onGpsStatusChanged(): ##,used,s/n,az,el" 06-08 23:23:25.147,D,GPS,22427,"onGpsStatusChanged(): 2,true,26.0,57.0,73.0" 06-08 23:23:25.147,D,GPS,22427,"onGpsStatusChanged(): 4,true,30.0,46.0,27.0" 06-08 23:23:25.147,D,GPS,22427,"onGpsStatusChanged(): 5,true,19.0,144.0,25.0" 06-08 23:23:25.155,D,GPS,22427,"onGpsStatusChanged(): 9,true,22.0,202.0,22.0" 06-08 23:23:25.155,D,GPS,22427,"onGpsStatusChanged(): 10,true,17.0,109.0,32.0" 06-08 23:23:25.155,D,GPS,22427,"onGpsStatusChanged(): 12,true,32.0,320.0,80.0" 06-08 23:23:25.155,D,GPS,22427,"onGpsStatusChanged(): 29,true,21.0,278.0,21.0" 06-08 23:23:25.155,D,GPS,22427,"onGpsStatusChanged(): 30,true,31.0,312.0,43.0" 06-08 23:23:25.163,D,GpsLocationProvider,1039,TTFF: 22457 06-08 23:23:25.194,I,GPS,22427,onGpsStatusChanged(): time to first fix in ms = 22457

Note that at fix time, you will be getting the current location, not where you once may have been. I think along with what you have already you can get there now. Or, check out how the pros do it here.

like image 27
DJC Avatar answered Nov 11 '22 16:11

DJC