因为一些特殊程序的要求需要检测 Home 键,activity 类方法:
1 public boolean dispatchKeyEvent (KeyEvent event)

可以重写不同的 keyevent 处理,但是 Home 不会 dispatch 过来,这就让检测 Home 键变得困难了。

PhoneWindowManager主要负责每个 activity 的 ui 刷新和部分事件的处理,其中:

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 void launchHomeFromHotKey() { if (mKeyguardMediator.isShowingAndNotHidden()) { // don't launch home if keyguard showing } else if (!mHideLockScreen && mKeyguardMediator.isInputRestricted()) { // when in keyguard restricted mode, must first verify unlock // before launching home mKeyguardMediator.verifyUnlock( new OnKeyguardExitResult() { public void onKeyguardExitResult( boolean success) { if (success) { try { ActivityManagerNative.getDefault().stopAppSwitches(); } catch (RemoteException e) { } sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY); startDockOrHome(); } } }); } else { // no keyguard stuff to worry about, just launch home! try { ActivityManagerNative.getDefault().stopAppSwitches(); } catch (RemoteException e) { } sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY); startDockOrHome(); } }

其中:

1 2 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY); startDockOrHome();

发送广播 Intent.ACTION_CLOSE_SYSTEM_DIALOGS,开始 Launch 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 class InnerRecevier extends BroadcastReceiver { static final String SYSTEM_DIALOG_REASON_KEY = "reason" ; static final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions" ; static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps" ; static 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 ) { Lg.i( "receive 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(); } } } } } }

以下是完整代码: package com.tiger.watcher;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


import com.tiger.utils.Lg;


/**
* watch the HOME key pressed, we only listen the HOME pressed <b>NOT Intercept
* </b>this pressed.<br>
* Tips:invoke {@link #startWatch()} in activity's onResume method and
* {@link #stopWatch()} in activity's onStop Method.
*
* @author idiottiger
*/
public class HomeWatcher {


static final String LOG_TAG = "HomeWatcher";


private Context mContext;


private IntentFilter mFilter;


private OnHomePressedListener mListener;


private InnerRecevier mRecevier;


public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}


/**
* set the home pressed listener, if set will callback the home pressed
* listener's method when home pressed.
*
* @param listener
*/
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
mRecevier = new InnerRecevier();
}


/**
* start watch
*/
public void startWatch() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}


/**
* stop watch
*/
public void stopWatch() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}


class InnerRecevier extends BroadcastReceiver {


static final String SYSTEM_DIALOG_REASON_KEY = "reason";


static final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";


static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";


static 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) {
Lg.i("receive action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
mListener.onHomePressed();
} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
mListener.onHomeLongPressed();
}
}
}
}
}


}


}

更多相关文章

  1. Android:Click,LongClick,Touch,KeyDown,KeyUp事件使用心得
  2. Android(安卓)自定义SeekBar(滑块跟随进度条移动)
  3. (新手)Android(安卓)studio 安装:错误和解决方法(updated on 200406)
  4. Android(安卓)Note - Broadcast Receiver
  5. android安全:flag FLAG_RECEIVER_REGISTERED_ONLY的意义
  6. Android(安卓)常见Action
  7. Android(安卓)监听主进程被杀
  8. Android四大组件基本介绍及其生命周期
  9. Android实现高斯模糊(也叫毛玻璃效果)

随机推荐

  1. Android简单、美观而且十分强大的日志工
  2. android SpannableString使用详解
  3. Android实现复制粘贴功能
  4. Android(安卓)加载服务器上的图片
  5. android计算器
  6. Android(安卓)带固定图片的EditText
  7. Android(安卓)判断Webview 的水平滚动
  8. android构建复合组件
  9. android 手势操作GestureDetector
  10. Android(安卓)多选列表