Shielding the Home key function of activity, dialog style activity, and alertdialog (Summary)

Source: Internet
Author: User

The Home key function for shielding activity, dialog style activity, and alertdialog is as follows:

1. Disable the Home key function of the activity.
@ Override
Public void onattachedtowindow (){
This. getwindow (). settype (windowmanager. layoutparams. type_keyguard );
Super. onattachedtowindow ();
}

2. Shielding the Home key function of a dialog style Activity
@ Override
Public void onattachedtowindow (){
This. getwindow (). settype (windowmanager. layoutparams. type_keyguard_dialog );
Super. onattachedtowindow ();
}
The reason for distinguishing this from activity is that if the dialog style activity uses the first method, the background of the dialog style activity will become black rather than transparent.

3. Shielding alertdialog's home key function
Alertdialog d = B. Create ();

D. getwindow (). settype (windowmanager. layoutparams. type_system_alert );

The Home key is not blocked.

In the sleep mode of a music player, to ensure that the user only performs operations on the current prompt box when the sleep shutdown prompt box is displayed, the Home Key is blocked, just like canceling the shutdown operation or confirming the shutdown operation must be performed when the mobile phone is going to shut down, unless the confirmation is completed to close this dialog box, the operation cannot be performed on other interfaces.

Sleepdialog = B. Create ();
Sleepdialog. getwindow (). settype (windowmanager. layoutparams. type_system_alert );
Sleepdialog. Show ();

Home Key listening function:

@ Override

Public Boolean onkeydown (INT keycode, keyevent event)

{// Todo auto-generated method stub

If (keyevent. keycode_home = keycode)

Android. OS. process. killprocess (Android. OS. process. mypid ());

Return super. onkeydown (keycode, event );

}

================================== Reload a Home Key listener function below:

This article is from the csdn Dandan blog. For more information, see the source:

Http://blog.csdn.net/dany1202/archive/2011/06/14/6543769.aspx

Support originality

Registration:

View plain
Intentfilter filter = new intentfilter (intent. action_close_system_dialogs );
Mclosesystemdialogsreceiver = new closesystemdialogsintentreceiver ();
Registerreceiver (mclosesystemdialogsreceiver, filter );

Listener:

View plain
Private class closesystemdialogsintentreceiver extends broadcastreceiver {
@ Override
Public void onreceive (context, intent ){

String reason = intent. getstringextra ("reason ");
Log. D (TAG, "closesystemdialogsintentreceiver reason is" + reason );
If ("homekey". Equals (reason )){
Log. D (TAG, "homekey click ===================== ");
// Editwidget. This. Finish ();
Keyhomeflag = true;
}

}
}

The above function can listen to the Home Key instead of other keys after the home key function is complete.

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.