Last active
March 1, 2023 17:49
-
-
Save xadh00m/1584a58ddb3d724cdd24 to your computer and use it in GitHub Desktop.
BackgroundManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to implement it in my app just register the call in service or needed something extra