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

时间:2022-11-30 23:25:08

一:AndroidHome键监听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键监听封装类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();
  }
 
}