Skip to content

Instantly share code, notes, and snippets.

@mobiRic
Forked from steveliles/Foreground.java
Last active May 26, 2016 12:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mobiRic/c8467eab1c2f3e59fcdb to your computer and use it in GitHub Desktop.
Save mobiRic/c8467eab1c2f3e59fcdb to your computer and use it in GitHub Desktop.
Class for detecting and eventing whether an Android app is currently foreground or background (requires API level 14+)
/*
* Copyright (C) 2014 Steve Liles
* Copyright (C) 2014 Glowworm Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sjl.util;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
/**
* Usage:
*
* 1. Get the Foreground Singleton, passing a Context or Application object unless you are sure that
* the Singleton has definitely already been initialised elsewhere.
*
* 2.a) Perform a direct, synchronous check: Foreground.isForeground() / .isBackground()
*
* or
*
* 2.b) Register to be notified (useful in Service or other non-UI components):
*
* <pre>
* Foreground.Listener myListener = new Foreground.Listener(){
* public void onBecameForeground(){
* // ... whatever you want to do
* }
* public void onBecameBackground(){
* // ... whatever you want to do
* }
* }
*
* public void onCreate(){
* super.onCreate();
* Foreground.get(this).addListener(listener);
* }
*
* public void onDestroy(){
* super.onCreate();
* Foreground.get(this).removeListener(listener);
* }
* </pre>
*/
public class Foreground implements Application.ActivityLifecycleCallbacks
{
public static final long CHECK_DELAY = 500;
public static final String TAG_FOREGROUND = Foreground.class.getName();
public interface Listener
{
public void onBecameForeground();
public void onBecameBackground();
}
private static Foreground instance;
private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;
/**
* Its not strictly necessary to use this method - _usually_ invoking get with a Context gives
* us a path to retrieve the Application and initialise, but sometimes (e.g. in test harness)
* the ApplicationContext is != the Application, and the docs make no guarantees.
*
* @param application
* @return an initialised Foreground instance
*/
public static Foreground init(Application application)
{
if (instance == null)
{
instance = new Foreground();
application.registerActivityLifecycleCallbacks(instance);
}
return instance;
}
public static Foreground get(Application application)
{
if (instance == null)
{
init(application);
}
return instance;
}
public static Foreground get(Context ctx)
{
if (instance == null)
{
Context appCtx = ctx.getApplicationContext();
if (appCtx instanceof Application)
{
init((Application) appCtx);
}
throw new IllegalStateException("Foreground is not initialised and "
+ "cannot obtain the Application object");
}
return instance;
}
public static Foreground get()
{
if (instance == null)
{
throw new IllegalStateException("Foreground is not initialised - invoke "
+ "at least once with parameterised init/get");
}
return instance;
}
public boolean isForeground()
{
return foreground;
}
public boolean isBackground()
{
return !foreground;
}
public void addListener(Listener listener)
{
listeners.add(listener);
}
public void removeListener(Listener listener)
{
listeners.remove(listener);
}
@Override
public void onActivityResumed(Activity activity)
{
paused = false;
boolean wasBackground = !foreground;
foreground = true;
if (check != null)
handler.removeCallbacks(check);
if (wasBackground)
{
Log.i(TAG_FOREGROUND, "went foreground");
for (Listener l : listeners)
{
try
{
l.onBecameForeground();
}
catch (Exception exc)
{
Log.e(TAG_FOREGROUND, "Listener threw exception!", exc);
}
}
}
else
{
Log.i(TAG_FOREGROUND, "still foreground");
}
}
@Override
public void onActivityPaused(Activity activity)
{
paused = true;
if (check != null)
handler.removeCallbacks(check);
handler.postDelayed(check = new Runnable()
{
@Override
public void run()
{
if (foreground && paused)
{
foreground = false;
Log.i(TAG_FOREGROUND, "went background");
for (Listener l : listeners)
{
try
{
l.onBecameBackground();
}
catch (Exception exc)
{
Log.e(TAG_FOREGROUND, "Listener threw exception!", exc);
}
}
}
else
{
Log.i(TAG_FOREGROUND, "still foreground");
}
}
}, CHECK_DELAY);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState)
{
}
@Override
public void onActivityStarted(Activity activity)
{
}
@Override
public void onActivityStopped(Activity activity)
{
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
@Override
public void onActivityDestroyed(Activity activity)
{
}
}
@mobiRic
Copy link
Author

mobiRic commented Jan 26, 2015

This is a great implementation of the standard boilerplate code written for many clients in the past.

Includes some nice enhancements, like the get(...) methods and using a CopyOnWriteArrayList.

@mobiRic
Copy link
Author

mobiRic commented Feb 22, 2015

Added Apache v2 licence as agreed with original author

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