Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Application is in Forefround or background

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 926
    Comment on it

    Application is in Forefround or background.

    Here I write the code to know whether our application is in background or forground. We use Application.ActivityLifecycleCallbacks interface and know status in onActivityResumed() and onActivityPaused() method.

    Just initialize it

     Foreground.init(getApplication()); 
    wherever you want in your project. and now status like this

    Foreground.get().isForeground();
    public class Foreground implements Application.ActivityLifecycleCallbacks {
    
    public static final long CHECKING_DELAY = 500;
    public static final String TAG = Foreground.class.getName();
    
    public interface ListenerForeOrBAck {
    
        public void onBecameForeground();
    
        public void onBecameBackground();
    
    }
    
    private static Foreground instance;
    
    private boolean foreground = false, paused = true;
    private Handler handler = new Handler();
    private List<ListenerForeOrBAck> listeners = new CopyOnWriteArrayList<ListenerForeOrBAck>();
    private Runnable checking;
    
    
    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(ListenerForeOrBAck listener){
        listeners.add(listener);
    }
    
    public void removeListener(ListenerForeOrBAck listener){
        listeners.remove(listener);
    }
    
    @Override
    public void onActivityResumed(Activity activity) {
        paused = false;
        boolean wasBackground = !foreground;
        foreground = true;
    
        if (checking != null)
            handler.removeCallbacks(checking);
    
        if (wasBackground){
            Log.i(TAG, "went foreground");
            for (ListenerForeOrBAck lis : listeners) {
                try {
                    lis.onBecameForeground();
                } catch (Exception e) {
                    Log.e(TAG, "Listener  exception!", e);
                }
            }
        } else {
            Log.i(TAG, "still foreground");
        }
    }
    
    @Override
    public void onActivityPaused(Activity activity) {
        paused = true;
    
        if (checking != null)
            handler.removeCallbacks(checking);
    
        handler.postDelayed(checking = new Runnable(){
            @Override
            public void run() {
                if (foreground && paused) {
                    foreground = false;
                    Log.i(TAG, "background");
                    for (ListenerForeOrBAck l : listeners) {
                        try {
                            l.onBecameBackground();
                        } catch (Exception e) {
                            Log.e(TAG, "Listener  exception!", e);
                        }
                    }
                } else {
                    Log.i(TAG, "still foreground");
                }
            }
        }, CHECKING_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) {}
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: