Android开发

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

本文主要是介绍Android监听Home键和Back键的区别介绍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一: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();
  }

}

这篇关于Android监听Home键和Back键的区别介绍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!