1

I'm trying to integrate a pin entry activity that appears and is required for entry whenever the user opens the app. Including after the app is sent to the background and then brought to the foreground.

This question highlights ways to detect when the app is sent to the background: How to detect when an Android app goes to the background and come back to the foreground

I could use a method from here and check if the app went to the background. Then in the onResume() method I can start the pin entry activity if the app was not in the foreground.

Given this is a pin entry activity used to increase security, would force starting an activity in this way be reliable (Are there any other ways I have overlooked a user could open the app)?

Community
  • 1
  • 1
willjgriff
  • 783
  • 2
  • 6
  • 11
  • If the App in question is yours, simply add a redirect in onResume. Check the Pin. – Knossos May 11 '15 at 14:17
  • 1
    Well, using `onResume()` could be tricky. If you start another `Activity` from `onResume()`, your original `Activity` will be immediately paused again. Then you enter your pin in the other `Activity`, and go back to your original one, in which case `onResume()` is called again... Not saying it can't be done, but be careful with this. – ci_ May 11 '15 at 14:22
  • 2
    @ci_ is correct, however, you can simply pass an extra that indicates that the pin has already been validated. If the extra is not detected, then launch the pin verifying activity. – ahodder May 11 '15 at 14:34

1 Answers1

0

Use Foreground.java class to check app is foreground or background

package com.mindsetapp;

   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;


public class Foreground implements Application.ActivityLifecycleCallbacks {

public static final long CHECK_DELAY = 500;
public static final String TAG = 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);
    try {


        if (wasBackground){
            Log.i(TAG, "went foreground");
            if (!MindSetApp.flag_mute) {


            MindSetApp.sound_flag=true;
            if( MindSetApp.sound_flag)
            {
                if(!MindSetApp.mPlayer.isPlaying())
                {
                    MindSetApp.mPlayer.start();
                }
            }
            }

            for (Listener l : listeners) {
                try {
                    l.onBecameForeground();
                } catch (Exception exc) {
                    Log.e(TAG, "Listener threw exception!", exc);
                }
            }
        } else {
            Log.i(TAG, "still foreground");

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@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;

                for (Listener l : listeners) {
                    try {
                        l.onBecameBackground();
                    } catch (Exception exc) {
                        Log.e(TAG, "Listener threw exception!", exc);
                    }
                }
            } else {
                Log.i(TAG, "still foreground"+MindSetApp.sound_flag);
            }
        }
    }, 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) {}
}
Jinal Patel
  • 699
  • 5
  • 15