Implement Passcode Lock for Android Application
<p>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 <a href="http://stackoverflow.com/questions/8249651/know-when-to-show-a-passcode-lock">this post</a>.</p>

<ol>
<li><p>Use a static variable and reset it in onSTop of each activity and check it again in the onStart 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.</p></li>
<li><p>I tried the other approach by using this method.</p>

<p>protected boolean isAppOnForeground(final Context context) {
        List appProcesses = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();</p>

<pre><code>    if (appProcesses == null) {
        return false;
    }

    final String packageName = context.getPackageName();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) &amp;&amp; 
             appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}
</code></pre></li>
</ol>

<p>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</p>

<p>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.</p>
