Skip to content

Instantly share code, notes, and snippets.

@xadh00m
Last active March 1, 2023 17:49
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xadh00m/1584a58ddb3d724cdd24 to your computer and use it in GitHub Desktop.
Save xadh00m/1584a58ddb3d724cdd24 to your computer and use it in GitHub Desktop.
BackgroundManager
package com.android.utils;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
// based on https://gist.github.com/steveliles/11116937
public class BackgroundManager implements Application.ActivityLifecycleCallbacks {
private static final Logger LOG = Logger.getLogger(BackgroundManager.class);
public static final long BACKGROUND_DELAY = 500;
private static BackgroundManager sInstance;
public interface Listener {
public void onBecameForeground();
public void onBecameBackground();
}
private boolean mInBackground = true;
private final List<Listener> listeners = new ArrayList<Listener>();
private final Handler mBackgroundDelayHandler = new Handler();
private Runnable mBackgroundTransition;
public static BackgroundManager get() {
if (sInstance == null) {
sInstance = new BackgroundManager();
}
return sInstance;
}
private BackgroundManager(Application application) {
application.registerActivityLifecycleCallbacks(this);
}
public void registerListener(Listener listener) {
listeners.add(listener);
}
public void unregisterListener(Listener listener) {
listeners.remove(listener);
}
public boolean isInBackground() {
return mInBackground;
}
@Override
public void onActivityResumed(Activity activity) {
if (mBackgroundTransition != null) {
mBackgroundDelayHandler.removeCallbacks(mBackgroundTransition);
mBackgroundTransition = null;
}
if (mInBackground) {
mInBackground = false;
notifyOnBecameForeground();
LOG.info("Application went to foreground");
}
}
private void notifyOnBecameForeground() {
for (Listener listener : listeners) {
try {
listener.onBecameForeground();
} catch (Exception e) {
LOG.error("Listener threw exception!", e);
}
}
}
@Override
public void onActivityPaused(Activity activity) {
if (!mInBackground && mBackgroundTransition == null) {
mBackgroundTransition = new Runnable() {
@Override
public void run() {
mInBackground = true;
mBackgroundTransition = null;
notifyOnBecameBackground();
LOG.info("Application went to background");
}
};
mBackgroundDelayHandler.postDelayed(mBackgroundTransition, BACKGROUND_DELAY);
}
}
private void notifyOnBecameBackground() {
for (Listener listener : listeners) {
try {
listener.onBecameBackground();
} catch (Exception e) {
LOG.error("Listener threw exception!", e);
}
}
}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {}
}
@rajuashok
Copy link

The getInstance call is making a call to a non-existent constructor.

@onlyAnkita
Copy link

how to implement it in my app just register the call in service or needed something extra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment