The home Key listens, shields, and simulates the home key, and listens to the home

Source: Internet
Author: User

The home Key listens, shields, and simulates the home key, and listens to the home
 

/*** Press the home Key to return the program to the background and run the * @ param context */private void imitatePressHome (Context context) {Intent intent = new Intent (Intent. ACTION_MAIN); intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); intent. addCategory (Intent. CATEGORY_HOME); context. startActivity (intent);}/*** register the home Key listener * @ param context */private void registerHomeReceiver (Context context) {context. registerReceiver (mHomeKeyEventReceiver, new IntentFilter (Intent. ACTION_CLOSE_SYSTEM_DIALOGS);}/*** home Key Processing */private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver () {String SYSTEM_REASON = "reason"; String SYSTEM_HOME_KEY = "homekey "; string SYSTEM_HOME_KEY_LONG = "recentapps"; @ Override public void onReceive (Context context, Intent intent) {String action = intent. getAction (); if (action. equals (Intent. ACTION_CLOSE_SYSTEM_DIALOGS) {String reason = intent. getStringExtra (SYSTEM_REASON); if (TextUtils. equals (reason, SYSTEM_HOME_KEY) {// indicates that the home key is pressed, and the program goes to the background Toast. makeText (context, "home pressed", 1000 ). show ();} else if (TextUtils. equals (reason, SYSTEM_HOME_KEY_LONG) {// means to press the home Key to display the list of recently used programs Toast. makeText (context, "home long pressed", 1000 ). show () ;}}};/*** home Key mask */@ Override public void onAttachedToWindow () {// TODO Auto-generated method stub this. getWindow (). setType (WindowManager. layoutParams. TYPE_KEYGUARD); super. onAttachedToWindow ();}


After 4.0, the Type of the window cannot be reset in both onCreate () and onAttachedToWindow (). It is invalid to use this method to shield the Home Key!
If you run the code above 4.0, java is reported. lang. illegalArgumentException: Window type can not be changed after the window is added. as for how to shield it, there is no good solution on the Internet now. mtk has a solution, and other platforms will not work .... unless you modify the underlying framework
Mtk solution:
Public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
Run in onCreate
This. getWindow (). setFlags (FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED );

: The android system disables the HOME key solution. You need to be able to listen to this event and shield it from being compatible with all versions of 22-42.

Declare permissions:
<Uses-permission android: name = "android. permission. SYSTEM_ALERT_WINDOW"/>
<Uses-permission android: name = "android. permission. INTERNAL_SYSTEM_WINDOW"/>

Declare functions and members in the activity class:

Runnable mDisableHomeKeyRunnable = new Runnable (){

@ Override
Public void run (){
DisableHomeKey ();

}
};

Handler mHandler = new Handler ();

Public void disableHomeKey ()
{
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_SYSTEM_ALERT );
}

Call in onCreate:
MHandler. postDelayed (mDisableHomeKeyRunnable, 200 );

In Android, how does one listen to the Back and Home keys?

Inherit Activity
Override OnKeyDown ()
Print the key value.
It seems to be on different mobile phones. The corresponding key values are different.
Obtain the Back and Home key values.
Then match the key value. Write what you want to do.
Back can be written as follows:
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_BACK ){

}
Return super. onKeyDown (keyCode, event );
}

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.