android - Is there a way to force start an activity when the app comes to foreground? -
i'm trying integrate pin entry activity appears , required entry whenever user opens app. including after app sent background , brought foreground.
this question highlights ways detect when app sent background: how detect when android app goes background , come foreground
i use method here , check if app went background. in onresume() method can start pin entry activity if app not in foreground.
given pin entry activity used increase security, force starting activity in way reliable (are there other ways have overlooked user open app)?
use foreground.java class check app 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; /** * not strictly necessary use method - _usually_ invoking * context gives path retrieve application , * initialise, (e.g. in test harness) applicationcontext * != application, , docs make no guarantees. * * @param application * @return 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 not initialised , " + "cannot obtain application object"); } return instance; } public static foreground get(){ if (instance == null) { throw new illegalstateexception( "foreground not initialised - invoke " + "at least once 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(); } } } (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; (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) {} }
Comments
Post a Comment