Detect when application is open / resume

I would like to register and check the user with the server every time the application is open / closed, regardless of whether it starts or resumes from the task box. Is there a way to do this while avoiding the need to call a function in each event?

Thanks!

+6
source share
4 answers

The answer is provided by d60402 here and Hanno Binder's suggestion to register activity callbacks with Application.registerActivityLifecycleCallbacks () led me to this solution.

I expanded the application and registered callbacks to the onPause and onStart activity methods, as shown below. In these methods, the timer starts / stops (one action is called when onPause is called, and a new one is entered where onStart is called). The "wasInBackground" flag switches when the application is installed that it is in the background / foreground (true / false). If the application was in the background when starting callback onStart, "appEntered" is called. If the time elapsed between the onPause and onStart callbacks is longer than the specified time (gives enough time for activity transitions), "appExited" is called where it is considered that the application session has ended.

public class MyApplication extends Application { public static final String LOG_TAG = "MyApp"; public boolean wasInBackground = true; private AppSession appSession; private Timer mActivityTransitionTimer; private TimerTask mActivityTransitionTimerTask; private final long MAX_ACTIVITY_TRANSITION_TIME_MS = 2000; // Time allowed for transitions Application.ActivityLifecycleCallbacks activityCallbacks = new Application.ActivityLifecycleCallbacks() { @Override public void onActivityResumed(Activity activity) { if (wasInBackground) { //Do app-wide came-here-from-background code appEntered(); } stopActivityTransitionTimer(); } @Override public void onActivityPaused(Activity activity) { startActivityTransitionTimer(); } ... }; @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(activityCallbacks); } public void startActivityTransitionTimer() { this.mActivityTransitionTimer = new Timer(); this.mActivityTransitionTimerTask = new TimerTask() { public void run() { // Task is run when app is exited wasInBackground = true; appExited(); } }; this.mActivityTransitionTimer.schedule(mActivityTransitionTimerTask, MAX_ACTIVITY_TRANSITION_TIME_MS); } public void stopActivityTransitionTimer() { if (this.mActivityTransitionTimerTask != null) { this.mActivityTransitionTimerTask.cancel(); } if (this.mActivityTransitionTimer != null) { this.mActivityTransitionTimer.cancel(); } this.wasInBackground = false; } private void appEntered() { Log.i(LOG_TAG, "APP ENTERED"); appSession = new AppSession(); } private void appExited() { Log.i(LOG_TAG, "APP EXITED"); appSession.finishAppSession(); // Submit AppSession to server submitAppSession(appSession); long sessionLength = (appSession.getT_close() - appSession.getT_open())/1000L; Log.i(LOG_TAG, "Session Length: " + sessionLength); } 
+7
source
+6
source

Good. I am posting my comments as an answer since the original user who asked the question found this really useful.

The problem with the answers above is that the application does not want to check and check when the action closes, but rather when the application starts or resumes. Thus, this can become problematic when you close or move between actions when you have an application open that will still call the oncreate () and onpause () functions.

This issue has also been discussed previously in stackoverflow. Below is the link.

How to determine when an Android app goes into the background and comes to the fore

There may be different ways to solve this problem. The above link gives more information on how you can solve it.

+1
source

You can use the oncreate() and onpause() methods.

0
source

Source: https://habr.com/ru/post/986165/


All Articles