Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:44:31+00:00 2026-05-27T01:44:31+00:00

I am developing an application that needs to display a passcode screen whenever a

  • 0

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:

The starting activity would call for the passcode check on startup, and each activity added the following functionality to their onPause method:

@Override
public void onPause() {
    super.onPause();

    if (!isFinishing()) {
    new PasscodeCheckTask(this.getApplicationContext(),this).execute();
    }
}

The PassocdeCheckTask looks like the following. It checks to see if the screen is off or the app is no longer in the background

public class PasscodeCheckTask extends AsyncTask<Void, Void, Boolean> {

    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<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;
    }
}

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:44:32+00:00Added an answer on May 27, 2026 at 1:44 am

    In onStop() of each activity, update a static data member with the time you left the activity. In onStart() of each activity, check that time, and if it exceeds some timeout threshold, display your authentication activity. Allow the user to set the timeout value, so that if they don’t want to be bothered every few seconds, they can control that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an application that needs to perform some processing on the user's Outlook
I am developing an application that needs to read back the whole frame from
I'm developing a Ruby on Rails application that needs to allow the user to
I am developing an application that needs to use regini (because of legacy reasons)
I'm developing an application that needs to interact over FTP. For this communication I
I am developing a application that needs to store data with many writes and
I am developing an application that needs to inform users (around 1800) on a
Hi I am developing an application that needs to work with a complex domain
I'm developing a small utility application that needs to detect whether another one has
I am developing an internally-facing application that needs to automatically authenticate users via Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.