Android Monitor lock screen key, home button

Source: Internet
Author: User

Project used, record, return key what the physical keys use onkeydown monitoring can be, but lock screen key, home button can not, here use Broadcast monitor both state changes

Intentfilter Fragment:

Mfilter.addaction (intent.action_close_system_dialogs);//home key            mfilter.addaction (intent.action_screen_on);  Open screen            mfilter.addaction (intent.action_screen_off);//Lock Screen            mfilter.addaction (intent.action_user_present);// Unlock

Onrecevier Fragment:

 public void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (intent.action_close_system_dialogs)) {String reason = Intent.getstringextra (system_dial                 Og_reason_key); if (reason! = null) {if (Mlistener! = null) {if (Reason.equals (system_dialog                         _reason_home_key)) {//short press the HOME key mlistener.onhomepressed ();                             } else if (reason. Equals (System_dialog_reason_recent_apps)) {                         Long press the Home key mlistener.onhomelongpressed ();              }}}} if (Action.equals (intent.action_screen_on)) {              Mlistener.onscreenon (); }else if (action.equals (Intent.action_screen_off)) {Mlistener.onscreenoff (); } else{//unlock//todo}

Finally, for the lock screen operation, due to the activity of the first onpause,onstop, so can not be like the previous anti-registration broadcast sample in the OnPause anti-registration, otherwise it will not receive lock screen operation

Android Monitor lock screen key, home button

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.