Other ideas to solve the Android 4.0.4 can not listen to the home key issues

Source: Internet
Author: User

Problem Description:

Since Android 4.0, developers are not able to monitor and screen HOME keys, for Keycode_home, the official description is as follows:

Home key. This key was handled by the framework and was never delivered to applications.

So, unless you modify the framework, you can not directly listen to the home button, online friends say listen log log, but now press the home button is not displayed log log, and different phone models even if the display will not be the same.

On the internet to find a lot of ways are not solved ... Helpless Ah.

In order to realize that the user has pressed the home button to handle the event, it is forced to use the following solution.

Solution:

1. Ideas

In the life cycle of the activity,

    

Each activity will go through the Onresume () method when it is started, and each activity will be taken at the end of the OnStop method, and the user will perform the OnStop () method after pressing the home key (see figure , which shows that when activity is not visible, it executes).

So, we can judge the state of all activity in each activity's OnStop () method, and if all of the activity is not visible (all performed OnStop ()), do what we want to do.

2. Logic

Custom application, where you define a global HashMap, define two methods, method one addactivitystatus (activity activity) to manage the state of the activity, method two Isallactivityalive () is used to query whether all activity is active.

In each activity of the Onresume () method, call Addactivitystatus (activity activity) to set the state of the current activity to true activation state, in OnStop () The Addactivitystatus method first calls the activity activity to set the state to false inactive, and then calls Isallactivityalive () to see if all activity is inactive. If all inactive (all activity has performed onstop ()), do what you want to do!!!

It's so messy.

3. Implement

1. Custom Application

 1 package com.houny.testhomekey.application; 2 3 Import Java.util.HashMap; 4 Import Java.util.Iterator; 5 Import Java.util.Map; 6 7 Import android.app.Activity; 8 Import android.app.Application; 9 public class Manageapplication extends application {One private hashmap<string, boolean> mapactivity = new H     Ashmap<string, boolean> ();//used to store activity-corresponding activation status of 14 private static manageapplication application = null;13       @Override15 public void OnCreate () {super.oncreate (); application = this;18}19 20/**21 * Put activity and status in list manage * * @param activity24 * @param isActivitied25 */26 public void Addactivitystatus (activity activity, Boolean isAlive) {Mapactivity.containskey (Activity.getclass (). Getna Me ()) {Mapactivity.remove (Activity.getclass ()) getName ()); Mapactivity.put (Activity.getclass ( ). GetName (), isAlive), or else {MAPACTIVITY.PUT (Activity.getclass (). GetName (), isAlive); 33}34 35}36 37/**38 * Determine if activity is active 39 * 40  * @return41 */42 public boolean isallactivityalive () {$ Boolean res = false;44 Iterator iter = Mapactivity.entryset (). iterator (); (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.ne XT (); Object key = Entry.getkey (); Boolean value = (Boolean) entry.getvalue (); F (value) {true;51}52}53 return res;55}56 Public St atic manageapplication getinstance () {application;59}60}

2. Set application to custom application in Mainfest

<application        android:name= "com.houny.testhomekey.application.ManageApplication"        android:allowbackup = "true"        android:icon= "@drawable/ic_launcher"        android:label= "@string/app_name"        android:theme= "@ Style/apptheme ">

3. Do all of the following in each activity:

3.1 in Oncreat () getapplication

    

    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Application = (manageapplication) this.getapplication ();        Setcontentview (r.layout.activity_main);        LOG.E ("Status", "OnCreate");        Findviewsbyid ();        Setviewslistener ();    }

3.2 Rewrite Onresume () to increase the state of the current activity to true activation state

    

@Override    protected void Onresume () {        log.e ("Status", "Onresume");        Application.addactivitystatus (Mainactivity.this, true);        Super.onresume ();    }

3.3 Rewrite OnStop (), change the status of the current activity to false inactive state and determine if all activity is inactive

    

@Override    protected void OnStop () {        log.e ("Status", "onStop");        Application.addactivitystatus (Mainactivity.this, false);        Boolean alived = Application.isallactivityalive ();                LOG.E ("All Activity Status", alived + "");                if (!alived) {            //todo the action you want to make        }        super.onstop ();    }

Ok!!! Done! O (∩_∩) o

Other ideas to solve the Android 4.0.4 can not listen to the home key issues

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.