Senén
Senén

Reputation: 21

Check that I don't receive the GPS signal since x period of time

I want to notify when I lose the GPS signal,I found this :
https://stackoverflow.com/a/8322160/1034806
But how can I compare two values of getTime() if onLocationChanged() is not launched?

Upvotes: 1

Views: 209

Answers (1)

Dmitry
Dmitry

Reputation: 425

The simplest way is send delayed message:

private static final int MSG_SIGNAL_LOST = 1;
private static final int GPS_SIGNAL_LOST_TIME = 10000; // 10 seconds

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MSG_SIGNAL_LOST:
            handleGpsSignalLost();
            break;
        }
    };
};

private void handleGpsSignalLost() {
    // GPS signal lost, handle here
}

private void postDelayedGpsLostCheck() {
    mHandler.removeMessages(MSG_SIGNAL_LOST);
    mHandler.sendEmptyMessageDelayed(MSG_SIGNAL_LOST, GPS_SIGNAL_LOST_TIME);
}

And just call postDelayedGpsLostCheck() method each time onLocationChanged() method is called. You even don't need to save and compare times.

Also don't forget to call

mHandler.removeMessages(MSG_SIGNAL_LOST);

when you go out from activity.

Upvotes: 1

Related Questions