How to monitor the android home Key

Source: Internet
Author: User

Listening to the Home Key has always plagued everyone, and it is also a very tangled problem. From the perspective of my blog clicks, I know how difficult this problem is to solve.

This blog has made another modification. The first part is to solve the monitoring of home in 2.2 or earlier versions, and the second part is the home monitoring of 4.0.x.

Part 1:

If you want to listen to the home key, there are several implementation methods

Method 1: android listens to the home Key and adds permissions. You must obtain the permission to process the home key event to perform operations on the home key,

Valid only for 2.2 and earlier systems.

1. Add Permissions

<Uses-permission android: name = "android. permission. DISABLE_KEYGUARD"/>

It means that the keyboard guard is out of service.

2. Reload the following two methods:@ Override

Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (KeyEvent. KEYCODE_HOME = keyCode ){

// Write the action or task to be executed
Android. OS. Process. killProcess (android. OS. Process. myPid ());

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

@ Override
Public void onAttachedToWindow (){
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );
Super. onAttachedToWindow ();
}

Method 2: You may need to modify the source code and the source code of the system. This is troublesome and is not recommended. You just need to use the first method.

Another alternative is the implementation method, that is, you can determine that you have clicked the Home key based on your business.


As this article was written earlier and has not been tested for Version 2.3 or later, I am sorry for the incorrect writing. Then I want to listen to the Home key and I should have to modify the source code. If you are familiar with android, try it. Find the location where the home key is blocked and send me a private message. Thank you. We hope that we can solve this problem that has been entangled in many programmers. It is a matter of reducing the number of tangle tasks for programmers. Let's work together to solve it.

Part 2:

Method 1

This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD) after the code is transplanted to 4.0.1; this line reports an error,

Error message: java. lang. IllegalArgumentException: Window type can not be changed after the window is added.

Two processing methods are available.

Method 1: Modify PhoneWindowManager. java

If (keyCode = KeyEvent. KEYCODE_HOME) {// send a broadcast here}

After the broadcast is received in the application, the source code is changed only when the system is used.

Method 2: Check the log and determine whether the Home Key is clicked Based on the log.

Private boolean isTesting = true; class CatchLogThread extends Thread {@ Overridepublic void run () {Process mLogcatProc = null; BufferedReader reader = null; String line; while (isTesting) {try {// obtain the logcat log information mLogcatProc = runtime.getruntime(cmd.exe c (new String [] {"logcat", "ActivityManager: I *: S "}); reader = new BufferedReader (new InputStreamReader (mLogcatProc. getInputStream (); while (line = reader. r EadLine ())! = Null) {if (line. indexOf ("android. intent. category. HOME ")> 0) {isTesting = falsew.handler.sendmessage(handler.obtainmessage(###runtime.getruntime(cmd.exe c (" logcat-c "); // Delete log break;} catch (Exception e) {e. printStackTrace () ;}}}; Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {Log. I (TAG, "Home key press"); // do something here };};

This method is from this blog post.

This method is recorded on my blog. After testing, modify the method and use it conveniently.

If any of you have a better solution, please trust me.

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.