Christian
Christian

Reputation: 942

Auto refresh button?

I have an app that gets the longitude and latitude on a push of a button. I want to know how to get it so I don't have to push the button every time that I want to refresh to see the location. I know that I can take out the button function and it will automatically update but I need it to only show when the button is selected to on. Is there a way that I can refresh the button without having to keep clicking it?

Upvotes: 0

Views: 806

Answers (2)

ahodder
ahodder

Reputation: 11439

private Handler mHandler = new Handler() {
    @Override public void handleMessage(Message msg) {
        update(); // The refresh stuffs
        mHandler.sendMessageDelayed(Message.obtain(mHandler, -1), 1L); // wait
    }
}

With this you wont need a button at all. In my opinion, I don't know why you have a button if you don't want to click it....

Upvotes: 0

Andrew Rasmussen
Andrew Rasmussen

Reputation: 15099

Rather than having the button call a refresh function, have it set a boolean value "isButtonPressed" to true. And then when the button is pressed again (turned off), set it back to false. Then somewhere in your application that gets called continuously (do you have a game loop? update function? something like that?) do this:

void Update() {
    if (isButtonPressed) {
        Refresh();
    }
}

Make sure you initialize the boolean value to false, set it to true when the button is pressed, and then set it to false when the button is pressed again. You can do that like this:

void ButtonPressedCallback() {
    isButtonPressed = !isButtonPressed;
}

This is all making the assumption that you are running a game and would have a game loop, or that you have some sort of update function that's called infinitely in your application until it exits. If not, consider looking at Android activities in order to do something like that.

Upvotes: 1

Related Questions