Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

rajuashok commented Nov 16, 2015

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

@onlyAnkita

This comment has been minimized.

Copy link

onlyAnkita commented Mar 4, 2016

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
You can’t perform that action at this time.