How to block (intercept) The Home button and other buttons in the Android App

Source: Internet
Author: User

Write an Acitivity, as shown below:
Key Aspect 1: block the HOME button and set FLAG_HOMEKEY_DISPATCHED in onAttachedToWindow.
Key Aspect 2: shield the buttons except Power and Home, overwrite the dispatchKeyEvent method, and return true.
Public class LockByKeyGuard extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
@ Override
Public void onAttachedToWindow (){
// Key: Set FLAG_HOMEKEY_DISPATCHED in onAttachedToWindow.
This. getWindow (). addFlags (WindowManager. LayoutParams. FLAG_HOMEKEY_DISPATCHED );
Super. onAttachedToWindow ();
}
 
@ Override
Public boolean dispatchKeyEvent (KeyEvent event ){
// Return true without responding to other keys
Return true;
}

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.