mushroom
mushroom

Reputation: 1945

Periodic Updates while App is Open in Android

How can I periodically run a method but only when the App is open?

This should continue to work even if the app transitions from one activity to another.

Alarms doesn't seem like it will work to me because it continues even if the app is closed.

Upvotes: 1

Views: 59

Answers (3)

fractalwrench
fractalwrench

Reputation: 4076

You can start a Sticky Service and create a Handler within that service. You can then post a runnable to the Handler like below:

Handler handler = new Handler();

Runnable runnable = new Runnable() {
    @Override public void run() { // run repeating code here
        handler.postDelayed(this, 5000); // delay 5s
    }
}

handler.post(runnable);

Upvotes: 0

RobVoisey
RobVoisey

Reputation: 1083

You could initialize a Thread in onStart and clean it up in onStop to ensure that it is only run when the activity is open. Your work will then occur on the new Thread, note that when using a thread, you cannot update UI without communicating back to the main thread.

To ensure this happens across all activities, consider inheriting from Activity, override onStart and onStop and then have all of your Activities inherit from that instead.

EDIT:

To deterine when an app has closed is tricky and depends on how your app works. I solved it by using the following code in a common Activity.

@Override
protected void onStart() {
    super.onStart();

    if(mUserNavigatedAwayFromApp) {
        mUserNavigatedAwayFromApp = false;

        // Stop Thread or whatever you need to do here
    }

    // In case we resume without stopping after starting another activity (e.g. if the newer activity is transparent)
    // We need to clear this flag for the next call to onStop
    mOnStopPredicted = false;
}

private boolean mOnStopPredicted = false;
private boolean mUserNavigatedAwayFromApp = false;

@Override
public void startActivityForResult(Intent intent, int resultCode) {
    // User is navigating forward, set this flag so that onStop does not log out the user
    mOnStopPredicted = true;

    super.startActivityForResult(intent, resultCode);
};

@Override
public void finish() {
    // User is navigating backward, set this flag so that onStop does not log out the user
    mOnStopPredicted = true;

    super.finish();
};

@Override
protected void onStop() {

    // We track when know about activity navigation and if the activity stops WITHOUT us knowing about it
    if(mOnStopPredicted == false) {
        mUserNavigatedAwayFromApp = true;
    }

    mOnStopPredicted = false;

    super.onStop();
}

Some solutions in this question may better suit your needs How to detect when an Android app goes to the background and come back to the foreground.

Upvotes: 1

GreenGodot
GreenGodot

Reputation: 6773

I would reccomend looking into Bound Services (as normal Services run even when the app is not on screen)

http://developer.android.com/guide/components/bound-services.html

Upvotes: 1

Related Questions