Home按鍵手勢監(jiān)聽
home 按鍵監(jiān)聽需要打開廣播進行監(jiān)聽渐夸,同時需要使用動態(tài)注冊坛掠。
static class HomeWatcherReceiver extends BroadcastReceiver {
private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "onReceive: action: " + action);
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
// android.intent.action.CLOSE_SYSTEM_DIALOGS
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
Log.i(TAG, "reason: " + reason);
if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
// 短按Home鍵
Log.i(TAG, "homekey");
} else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
// 長按Home鍵 或者 activity切換鍵
Log.i(TAG, "long press home key or activity switch");
} else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
// samsung 長按Home鍵
Log.i(TAG, "assist");
}
}
}
}
分別進行廣播的注冊和注銷瓣窄,在onResume和onPause中
private static void registerHomeKeyReceiver(Context context) {
Log.i(TAG, "registerHomeKeyReceiver");
mHomeKeyReceiver = new HomeWatcherReceiver();
final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.registerReceiver(mHomeKeyReceiver, homeFilter);
}
private static void unregisterHomeKeyReceiver(Context context) {
Log.i(TAG, "unregisterHomeKeyReceiver");
if (null != mHomeKeyReceiver) {
context.unregisterReceiver(mHomeKeyReceiver);
}
}
又是一條分割線~
說明我又撿到寶貝了??
雖然不能判斷是不是長按
當然了……
也是因為我孤落寡聞??
加粗加粗
onUserLeaveHint
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.
This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
當前臺的activity被至于后臺時溢吻,onStop()方法不一定會被調(diào)用乌庶,此時onUserLeaveHint排上用場~ 感覺這個方法就是為了按下Home鍵而生的有木有~
onUserInteraction
Called whenever a key, touch, or trackball event is dispatched to the activity. Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and onUserLeaveHint() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
All calls to your activity's onUserLeaveHint() callback will be accompanied by calls to onUserInteraction(). This ensures that your activity will be told of relevant user activity such as pulling down the notification pane and touching an item there.
Note that this callback will be invoked for the touch down action that begins a touch gesture, but may not be invoked for the touch-moved and touch-up actions that follow.
當你想監(jiān)聽用戶在正在運行的activity上進行了其他操作的時候,執(zhí)行此方法左痢。和onUserLeaveHint()配合進行狀態(tài)欄通知的管理碗淌,他們是一對好基友。確保你的activity將獲取到相關(guān)用戶活動抖锥,例如下拉通知點擊。觸摸將調(diào)用此回調(diào)碎罚,touch-moved 和touch-up不會調(diào)用此回調(diào)磅废。