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