Skip to content Skip to sidebar Skip to footer

Android: "application Level" Pause And Resume

I've been trying to get Application Level Pause and Resume similar to an activity's onPause and onResume. I know there's no API that has this functionality. I try to follow this

Solution 1:

Another solution to the problem would be to just keep track of the count of onStart() and onStop() calls from every activity. Example:

First, create a class to hold the counts:

publicclassActiveActivitiesTracker {
    privatestaticint sActiveActivities = 0;

    publicstaticvoidactivityStarted()
    {
        if( sActiveActivities == 0 )
        {
            // TODO: Here is presumably "application level" resume
        }
        sActiveActivities++;
    }

    publicstaticvoidactivityStopped()
    {
        sActiveActivities--;
        if( sActiveActivities == 0 )
        {
            // TODO: Here is presumably "application level" pause
        }
    }
}

Then in every activity, simply call the activityStarted() and activityStopped() methods:

@OverridepublicvoidonStart() {
    super.onStart();
    ActiveActivitiesTracker.activityStarted();
}

@OverridepublicvoidonStop() {
    super.onStop();
    ActiveActivitiesTracker.activityStopped();
}

Solution 2:

I had the same problem. My aim was to lock the App, if the user abandons it. A simple aim, which i thought would be easy to implement. But all the solutions I found were way to complex. So I came to a simple solution: A time based lock.

Basically it works like this:

  • Start countdown to lock app in onPause
  • Stop countdown in onResume
  • If onResume is not called in time, change to locked

Therefor I created a small little class:

publicclassApplicationLock {

privatestaticfinalStringTAG= ApplicationLock.class.getSimpleName();
privatestaticfinalintLOCK_TIME=1000; //lock after a secondprivatestaticbooleanlock=true; //default is lockedprivatestaticHandlerhandler=newHandler();
privatestaticRunnablerunnable=newRunnable() {
    @Overridepublicvoidrun() {
        lock = true;
        Log.i("ActivityTracker", "App locked");
    }
};

publicstaticbooleanactivityStarted()
{
    handler.removeCallbacks(runnable);
    if(lock)
    {
        Log.i(TAG, "App resumed - LOCKED");
        returntrue;
    }else{
        Log.i(TAG, "App resumed - NOT LOCKED");
        returnfalse;
    }
}

publicstaticvoidactivityStopped()
{
    handler.postDelayed(runnable, LOCK_TIME);
    Log.i(TAG, "App paused - Starting countdown");

}

Just call activityStopped() in your activities onPause() and activityStarted() in onResume(). Check the result of activityStarted(). If it returns true, lock your app. If the orientation of the app is changed, onResume will be called very quickly after onPause, so the app will not lock.

This solution might not fit every scenario, but in my case it was the best solution. Additionally you can change the countdown, to increase the user experience (The user pressed a wrong button and returns to the app in a few seconds, no need to lock the app). Hope this is useful to someone else.

Solution 3:

I have done something very similar to this in an app which used a service that provided GPS functions by several activities. The idea was to only have the service there when one of the activities that used it is visible, and not there when none are visible. In your case, every activity would hook into a service, and you will know when the entire application was paused or resumed by hooking into the service's onCreate() and onDestroy() methods.

Here is a stripped-down example:

Components needed (these could probably be placed into a utility class if you want to reuse them, or I just had them for each activity class):

privatebooleanmAppActiveServiceBound=false;
privateAppActiveServicemAppActiveService=null;
privateServiceConnectionmAppActiveConnection=newServiceConnection() {
    publicvoidonServiceConnected( ComponentName className, IBinder service ) {
        mAppActiveService = ( (AppActiveService.AppActiveBinder) service ).getService();
    }
    publicvoidonServiceDisconnected( ComponentName className ) {
        mAppActiveService = null;
    }
};

Then in your onStart() and onStop() methods for each activity:

@OverridepublicvoidonStart() {
    super.onStart();
    mAppActiveServiceBound = bindService( newIntent( this, AppActiveService.class ), mAppActiveConnection, Context.BIND_AUTO_CREATE );
}

@OverridepublicvoidonStop() {
    super.onStop();
    if( mAppActiveServiceBound ) {
        unbindService( mAppActiveConnection );
        mAppActiveServiceBound = false;
    }
}

And finally, the service itself:

publicclassAppActiveServiceextendsService {
    // Receives interactions from clients:privatefinalIBindermBinder=newAppActiveBinder();

    /**
     * Provides a handle to the bound service.
     */publicclassAppActiveBinderextendsBinder {
        AppActiveService getService() {
            return AppActiveService.this;
        }
    }

    @OverridepublicvoidonCreate(){
        // TODO: Here is presumably "application level" resume
    }

    @OverridepublicvoidonDestroy(){
        // TODO: Here is presumably "application level" pause
    }
}

Post a Comment for "Android: "application Level" Pause And Resume"