欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android监听Home键和Back键的区别介绍

程序员文章站 2023-08-29 18:36:25
一:android 中home键监听和back键监听的区别: (1).在android中,当按下home键的时候,默认情况下stop前台的activity,即activi...

一:android 中home键监听和back键监听的区别:

(1).在android中,当按下home键的时候,默认情况下stop前台的activity,即activity设置成停止状态【onstop()】,而不是销毁状态【ondestory()】。如果再次启动该activity,不是调用oncreate()方法,而是调用onsavedinstancestate方法。则是从onrestart()开始-onstart()-onresume()。
(2).当按下back键则不同,back键默认finish前台的activity,即activity的状态为ondestory为止,再次启动该activity则从oncreate开始,不会调用onsavedinstancestate方法。
总而言之:按下home键会返回桌面,而back则会返回前一个activity。

在这里back键的实现方法就不赘述了,主要讲解一下home键的实现方式吧。以下是我找的参考资料自己思考的。请大家给出建议。
home键监听的实现方法:
(1).在onresum里面注册广播,onpause里面注销广播。
(2).在广播中拦截intent.action_close_system_dialogs 这个action ,通过获取reason字段 来判断长按 还是单击home键。
代码如下:

(1).home键监听封装类:

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键监听封装
 * 
 * @author way
 * 
 */
public class homewatcher {

  // 回调接口
  public interface onhomepressedlistener {

    public void onhomepressed();

    public void onhomelongpressed();
  }

  private static final string tag = "homewatcher";
  /** 上下文 */
  private context mcontext;
  /** 过滤器 */
  private intentfilter mfilter;
  /** 接口 */
  private onhomepressedlistener mlistener;
  /** 广播接收者 */
  private innerrecevier mrecevier;

  public homewatcher(context context) {
    mcontext = context;
    mrecevier = new innerrecevier();
    mfilter = new intentfilter(intent.action_close_system_dialogs);
  }

  /**
   * 设置监听
   * 
   * @param listener
   */
  public void setonhomepressedlistener(onhomepressedlistener listener) {
    mlistener = listener;
  }

  /**
   * 开始监听,注册广播
   */
  public void startwatch() {
    if (mrecevier != null) {
      mcontext.registerreceiver(mrecevier, mfilter);
    }
  }

  /**
   * 停止监听,注销广播
   */
  public void stopwatch() {
    if (mrecevier != null) {
      mcontext.unregisterreceiver(mrecevier);
    }
  }

  /**
   * 广播接收者
   */
  private class innerrecevier extends broadcastreceiver {
    final string system_dialog_reason_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_reason_key);
        if (reason != null) {
          log.i(tag, "action:" + action + ",reason:" + reason);
          if (mlistener != null) {
            if (reason.equals(system_dialog_reason_home_key)) {
              // 短按home键
              mlistener.onhomepressed();
            } else if (reason
                .equals(system_dialog_reason_recent_apps)) {
              // 长按home键
              mlistener.onhomelongpressed();
            }
          }
        }
      }
    }
  }
}

(2).mainactivity类:

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);
    // 注册广播
    mhomewatcher.startwatch();
    super.onresume();
  }

  @override
  protected void onpause() {
    mhomewatcher.setonhomepressedlistener(null);
    // 注销广播
    mhomewatcher.stopwatch();
    super.onpause();
  }

  @override
  public void onhomepressed() {
    // todo
    toast.maketext(this, "短按home键,实现自己的逻辑", toast.length_short).show();

  }

  @override
  public void onhomelongpressed() {
    // todo
    toast.maketext(this, "长按home键,实现自己的逻辑", toast.length_short).show();

  }

  @override
  public boolean onkeydown(int keycode, keyevent event) {
    if ((keycode == keyevent.keycode_back)) {
      system.out.println("按下了back键 onkeydown()");
      return false;
    } else {
      return super.onkeydown(keycode, event);
    }

  }

  @override
  public void onbackpressed() {
    // super.onbackpressed()会自动调用finish()方法,关闭
    super.onbackpressed();
  }

}