Example code for Android shielding and capturing the home key _android

Source: Internet
Author: User
Tags stub

In Level5 above (included), the method in the Activity class: Publicvoid Onattachedtowindow () captures the home key.

The specific code is as follows:

Mainactivity.java

 package com.lingdududu.test; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.view.KeyEvent; 
Import Android.view.WindowManager; 
 
Import Android.widget.Toast; The public class Mainactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '. * Private Boolean created 
 Mekey = false; 
  public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 Setcontentview (R.layout.main); @Override public void Onattachedtowindow () {//TODO auto-generated Method stub if (!catchhomekey) {th 
  Is.getwindow (). SetType (WindowManager.LayoutParams.TYPE_KEYGUARD); 
 } Super.onattachedtowindow (); @Override public boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method stub if (key 
  Code = = keyevent.keycode_home) {toast.maketext (Mainactivity.this, "You clicked the Home button", Toast.length_long). Show (); 
 Return Super.onkeydown (KeyCode, event); } 
} 

When we click on the Home button, the program does not return to the main interface, but stays on the current page, and can capture the toast display information.

Effect Chart:

The above is an example of Android getting, shielding home keys, hoping to help small partners in need.

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.