Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: "Application level" Pause and Resume [closed]

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 post: http://curioustechizen.blogspot.com/2012/12/android-application-level-pause-and.html

But I've had no luck so far.

Has anyone been able to achieve this? What paradigm did you use?

Let me know if you need me to paste some code into this question. Thanks for the help

like image 324
TMacGyver Avatar asked Jan 03 '14 20:01

TMacGyver


People also ask

What does it mean when an app is paused?

App pause is a feature in Android Q which allows us as users to pause a specific application for that day. Once an application is in pause state, you will not receive notifications from the app for the rest of the day.

When closing an app on Android what is happening in the backend?

In general, there's no such thing as closing applications in Android: the user just stops using the app. It's up to the programmer to make sure that the user does not mention process creation and termination.


2 Answers

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:

public class ActiveActivitiesTracker {
    private static int sActiveActivities = 0;

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

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

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

@Override
public void onStart() {
    super.onStart();
    ActiveActivitiesTracker.activityStarted();
}

@Override
public void onStop() {
    super.onStop();
    ActiveActivitiesTracker.activityStopped();
}
like image 80
paulscode Avatar answered Sep 28 '22 12:09

paulscode


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:

public class ApplicationLock {

private static final String TAG = ApplicationLock.class.getSimpleName();
private static final int LOCK_TIME = 1000; //lock after a second
private static boolean lock = true; //default is locked
private static Handler handler = new Handler();
private static Runnable runnable = new Runnable() {
    @Override
    public void run() {
        lock = true;
        Log.i("ActivityTracker", "App locked");
    }
};

public static boolean activityStarted()
{
    handler.removeCallbacks(runnable);
    if(lock)
    {
        Log.i(TAG, "App resumed - LOCKED");
        return true;
    }else{
        Log.i(TAG, "App resumed - NOT LOCKED");
        return false;
    }
}

public static void activityStopped()
{
    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.

like image 33
AlbAtNf Avatar answered Sep 28 '22 12:09

AlbAtNf