My application has sensitive user information and we need to implement a passcode screen to be displayed whenever the user opens the application. Here are the two approaches I tried after reading this post.
Use a static variable and reset it in
onStop()
of each activity and check it again in theonStart()
of each activity and show the passcode screen if the time crossed a minimum threshhold say 1-2 secs. The problem with this approach is that my application also uses intents to call camera and barcode scanners and the users may spend longer periods in these external apps. I can increase the threshold in this case but it makes the calculations complicated and is not a very good solution.I tried the other approach by using this method.
protected boolean isAppOnForeground(final Context context) { List<RunningAppProcessInfo> appProcesses = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses(); if (appProcesses == null) { return false; } final String packageName = context.getPackageName(); for (RunningAppProcessInfo appProcess : appProcesses) { if ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) && appProcess.processName.equals(packageName)) { return true; } } return false; }
But this will always return true when I check for it in the onStart method of each activity since the process already started by the time it is in onStart
Is there any other approach that I can take to display a passcode when ever the user opens the application? It should be displayed even when user clicks on home screen to go out of the app and then comes back to the app from recent apps.