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

  • SEARCH
  • Home
  • 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 7078101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:30:18+00:00 2026-05-28T06:30:18+00:00

I am currently trying to listen for when the power button is pressed. Ultimately

  • 0

I am currently trying to listen for when the power button is pressed. Ultimately I would like to have some code run when the power button is pressed twice, to check whether the screen is locked or unlocked. I currently have this:

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//prevent phone from being locked
}

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
        case KeyEvent.KEYCODE_POWER:
        {
            Toast.makeText(getBaseContext(), "Power button pressed", Toast.LENGTH_LONG).show();

            return true;
        }
        case KeyEvent.KEYCODE_MENU:
            Toast.makeText(getBaseContext(), "Menu Button Pressed", Toast.LENGTH_LONG).show();

            return true;
    }
    return super.onKeyDown(keyCode, event);
}

The code works fine to toast for the menu keyevent but doesn’t do anything for the power key event. Any ideas?

  • 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-28T06:30:19+00:00Added an answer on May 28, 2026 at 6:30 am

    Although I couldn’t capture the hardware key I ended up being able guess that the power key was pressed by using a broadcast receiver that listens for whether the screen is turned off or on. I used:

                if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                        //do something 
                }
    
                else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                        //do something else
                }
    

    I was able to be more sure that the power button was being pressed by having a count that would timeout after a couple second. This way I have the user press the power button three times to ensure that proper behavior.

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

Sidebar

Related Questions

I'm currently trying to read in an XML file, make some minor changes (alter
I'm currently trying out db4o (the java version) and I pretty much like what
I'm currently trying to add some preferences to a Firefox add-on. To do so,
I'm trying to find a way to have a page listen to which users
I have a power-shell script with which I am trying to back up a
I am currently trying to optimize a function I have and am looking to
I have a piece of C network software that currently works in listen and
i'm currently trying to set up something like this: a server side windows wcf
I'm currently trying to run a classic ASP application which I've been given source
I'm currently trying to find a PDF library which will run without a running

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.