Android Monitor the difference between the home key and the back key _android

Source: Internet
Author: User

One: The difference between the home key monitor and the back key monitor in Android:

(1). In Android, when the home key is pressed, by default the activity of the stop foreground, that is, the activity is set to stop State "onStop ()" Instead of destroying the State "ondestory ()". If you start the activity again, instead of calling the OnCreate () method, you call the Onsavedinstancestate method. The-onstart ()-onresume () is started from Onrestart ().
(2). When you press the back key, the return key defaults to the activity of the foreground, that is, the activity's status is ondestory, and the activity starts again from OnCreate, The Onsavedinstancestate method is not invoked.
All in all: Pressing the HOME key will return to the desktop, and back will return to the previous activity.

Here the back key implementation method is not to repeat, the main explanation of the home key to achieve the way it. Here are some of the references I've been looking for myself. Please give us some advice.
How to implement the Home key listener:
(1). Register the broadcast inside the Onresum, OnPause inside the cancellation broadcast.
(2). Intercept Intent.action_close_system_dialogs This ACTION on the radio and determine whether to click the home key by getting the reason field.
The code is as follows:

(1). The home key listens on the encapsulation class:

Package com.scd.homewatcher.util;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;

Import Android.util.Log;  /** * Home Key Listening Package * * @author Way * */public class Homewatcher {//Callback interface public interface Onhomepressedlistener

    {public void onhomepressed ();
  public void onhomelongpressed ();
  private static final String TAG = "Homewatcher";
  /** context * * MCONTEXT;
  /** Filter * * Private intentfilter mfilter;
  /** interface * * Private Onhomepressedlistener Mlistener;

  /** Broadcast Receiver * * Private innerrecevier mrecevier;
    Public Homewatcher {mcontext = context;
    Mrecevier = new Innerrecevier ();
  Mfilter = new Intentfilter (intent.action_close_system_dialogs);  /** * Set Monitor * * @param listener/public void Setonhomepressedlistener (Onhomepressedlistener listener)
  {Mlistener = listener;
   /** * Start listening, register broadcast*/public void Startwatch () {if (mrecevier!= null) {Mcontext.registerreceiver (mrecevier, Mfilter); }/** * Stop listening, logout broadcast/public void stopwatch () {if (mrecevier!= null) {MCONTEXT.UNREGISTERRECEIV
    ER (mrecevier); }/** * Broadcast receiver/private class Innerrecevier extends Broadcastreceiver {final String System_dialog_rea
    Son_key = "Reason";
    Final String system_dialog_reason_global_actions = "Globalactions";
    Final String System_dialog_reason_recent_apps = "Recentapps";

    Final String System_dialog_reason_home_key = "HomeKey";
      @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_dialog_reaso
        N_key);
          if (reason!= null) {LOG.I (TAG, "action:" + action + ", Reason:" + reason); if (Mlistener!= null) {if (reaSon.equals (System_dialog_reason_home_key)) {//Short Press Home key mlistener.onhomepressed (); else if (reason. Equals (System_dialog_reason_recent_apps)) {//Long press home key ML
            Istener.onhomelongpressed ();

 }
          }
        }
      }
    }
  }
}

(2). Mainactivity class:

Package com.scd.homewatcher;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.KeyEvent;

Import Android.widget.Toast;
Import Com.scd.homewatcher.util.HomeWatcher;

Import Com.scd.homewatcher.util.HomeWatcher.OnHomePressedListener;

  public class Mainactivity extends activity implements Onhomepressedlistener {private Homewatcher mhomewatcher;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.layout.activity_main);
    @Override protected void Onresume () {mhomewatcher = new Homewatcher (this);
    Mhomewatcher.setonhomepressedlistener (this);
    Registered broadcast Mhomewatcher.startwatch ();
  Super.onresume ();
    } @Override protected void OnPause () {mhomewatcher.setonhomepressedlistener (null);
    Cancellation of broadcast mhomewatcher.stopwatch ();
  Super.onpause (); @Override public void onhomepressed () {//TODO Toast.maketext (This, "short press the home key to implement your own logic", Toast.leNgth_short). Show (); @Override public void onhomelongpressed () {//TODO Toast.maketext (This, "long press the home key to implement your own logic", toast.length_sh

  ORT). Show ();
      @Override public boolean onKeyDown (int keycode, keyevent event) {if ((keycode = = Keyevent.keycode_back)) {
      System.out.println ("Press the Back Key OnKeyDown ()");
    return false;
    else {return Super.onkeydown (KeyCode, event); @Override public void onbackpressed () {//super.onbackpressed () automatically calls the finish () method to close super.onbackpressed
  ();
 }

}

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.