How can I check the current status of the GPS receiver?

前端 未结 17 1593
时光取名叫无心
时光取名叫无心 2020-11-22 08:53

How can I check the current status of the GPS receiver? I already checked the LocationListener onStatusChanged method but somehow it seems that is not working,

相关标签:
17条回答
  • 2020-11-22 09:47

    new member so unfortunately im unable to comment or vote up, however Stephen Daye's post above was the perfect solution to the exact same problem that i've been looking for help with.

    a small alteration to the following line:

    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;
    

    to:

    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < (GPS_UPDATE_INTERVAL * 2);
    

    basically as im building a slow paced game and my update interval is already set to 5 seconds, once the gps signal is out for 10+ seconds, thats the right time to trigger off something.

    cheers mate, spent about 10 hours trying to solve this solution before i found your post :)

    0 讨论(0)
  • 2020-11-22 09:47

    If you do not need an update on the very instant the fix is lost, you can modify the solution of Stephen Daye in that way, that you have a method that checks if the fix is still present.

    So you can just check it whenever you need some GPS data and and you don't need that GpsStatus.Listener.

    The "global" variables are:

    private Location lastKnownLocation;
    private long lastKnownLocationTimeMillis = 0;
    private boolean isGpsFix = false;
    

    This is the method that is called within "onLocationChanged()" to remember the update time and the current location. Beside that it updates "isGpsFix":

    private void handlePositionResults(Location location) {
            if(location == null) return;
    
            lastKnownLocation = location;
            lastKnownLocationTimeMillis = SystemClock.elapsedRealtime();
    
            checkGpsFix(); // optional
        }
    

    That method is called whenever I need to know if there is a GPS fix:

    private boolean checkGpsFix(){
    
        if (SystemClock.elapsedRealtime() - lastKnownLocationTimeMillis < 3000) {
            isGpsFix = true;
    
        } else {
            isGpsFix = false;
            lastKnownLocation = null;
        }
        return isGpsFix;
    }
    

    In my implementation I first run checkGpsFix() and if the result is true I use the variable "lastKnownLocation" as my current position.

    0 讨论(0)
  • 2020-11-22 09:48

    You could try using LocationManager.addGpsStatusListener to get updated when the GPS status changes. It looks like GPS_EVENT_STARTED and GPS_EVENT_STOPPED might be what you're looking for.

    0 讨论(0)
  • 2020-11-22 09:49

    I know this is a little late. However why not use the NMEAListener if you want to know if you have a fix. From what I've read, the NMEAListener will give you the NMEA sentences and from there you pick the correct sentence.

    The RMC sentence contains the fix status which is either A for OK or V for warning. The GGA sentence contains the Fix Quality (0 invalid, 1 GPS or 2 DGPS)

    I can't offer you any java code as I'm only just starting out with Android, but I have done a GPS library in C# for Windows apps, which I'm looking to use with Xamarin. I only came across this thread because I was looking for provider information.

    From what I've read so far about the Location object I'm not all that comfortable about methods like getAccuracy() and hasAccuracy(). I'm used to extracting from the NMEA sentences HDOP and VDOP values to determine how accurate my fixes are. Its quite common to have a fix, but have a lousy HDOP which means your horizontal accuracy is not very good at all. For example sitting at your desk debugging with an external Bluetooth GPS device hard up against a window, you are quite likely to get a fix, but very poor HDOP and VDOP. Place your GPS device in a flower pot outside or something similar or add an external aerial to the GPS and immediately you get good HDOP and VDOP values.

    0 讨论(0)
  • 2020-11-22 09:55

    so many posts...

    GpsStatus.Listener gpsListener = new GpsStatus.Listener() {
                            public void onGpsStatusChanged(int event) {
                                if( event == GpsStatus.GPS_EVENT_FIRST_FIX){
                                    showMessageDialog("GPS fixed");
                                }
                            }
                     };
    

    adding this code, with addGpsListener... showMessageDialog ... just shows a standard dialog window with the string

    did the job perfectly for me :) thanks a lot :=) (sry for this post, not yet able to vote)

    0 讨论(0)
提交回复
热议问题