Note that there are some explanatory texts on larger screens.

plurals
  1. POKnow when to show a passcode lock
    text
    copied!<p>I am developing an application that needs to display a passcode screen whenever a user leaves the app and comes back (be it through a screen lock, or going back to the home screen through the back or home button). I had it working using the following:</p> <p>The starting activity would call for the passcode check on startup, and each activity added the following functionality to their onPause method:</p> <pre><code>@Override public void onPause() { super.onPause(); if (!isFinishing()) { new PasscodeCheckTask(this.getApplicationContext(),this).execute(); } } </code></pre> <p>The PassocdeCheckTask looks like the following. It checks to see if the screen is off or the app is no longer in the background</p> <pre><code>public class PasscodeCheckTask extends AsyncTask&lt;Void, Void, Boolean&gt; { public static final int CHECK_PASSCODE = 0; private Context mActivityApplicationContext; private Context mActivityContext; public PasscodeCheckTask(Context applicationContext, Context activityContext){ mActivityApplicationContext = applicationContext; mActivityContext = activityContext; } @Override protected Boolean doInBackground(Void... params) { Boolean result = false; if (!((PowerManager)mActivityApplicationContext.getSystemService(android.content.Context.POWER_SERVICE)).isScreenOn() || !isAppOnForeground(mActivityApplicationContext)) { result = true; } return result; } @Override protected void onPostExecute(Boolean result) { if (result) { // Start passcode activity to check for passcode /* CODE HERE */ ((Activity)mActivityContext).startActivityForResult(intent, CHECK_PASSCODE); } } protected boolean isAppOnForeground(final Context context) { List&lt;RunningAppProcessInfo&gt; 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) &amp;&amp; appProcess.processName.equals(packageName)) { return true; } } return false; } } </code></pre> <p>The Passcode activity would finish when done, and the calling activity would moveTaskToBackground(true) if the passcode didn't pass. This system worked beautifully until I tried it on an HTC Evo with mikg ROM. For some reason, the appProcess.importance never showed up as IMPORTANCE_FOREGROUND. It was always IMPORTANCE_BACKGROUND. Thus, the passcode would ALWAYS be brought up, even though the app never went into the background.</p> <p>I tried DropBox on that phone (which has a passcode lock as well), and it worked beautifully. I can't seem to find a different way to know when an app has gone to the background, or if it is being brought back from the background. Any ideas on how to make this work?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload