1

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?

1 Answer 1

1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.