Home的好基友啊哈哈

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)磅废。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市荆烈,隨后出現(xiàn)的幾起案子拯勉,更是在濱河造成了極大的恐慌,老刑警劉巖憔购,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宫峦,死亡現(xiàn)場離奇詭異,居然都是意外死亡玫鸟,警方通過查閱死者的電腦和手機导绷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來屎飘,“玉大人妥曲,你說我怎么就攤上這事贾费。” “怎么了檐盟?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵褂萧,是天一觀的道長。 經(jīng)常有香客問我葵萎,道長导犹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任羡忘,我火速辦了婚禮谎痢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘壳坪。我一直安慰自己舶得,他們只是感情好,可當我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布爽蝴。 她就那樣靜靜地躺著沐批,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蝎亚。 梳的紋絲不亂的頭發(fā)上九孩,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天,我揣著相機與錄音发框,去河邊找鬼躺彬。 笑死,一個胖子當著我的面吹牛梅惯,可吹牛的內(nèi)容都是我干的宪拥。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼铣减,長吁一口氣:“原來是場噩夢啊……” “哼她君!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起葫哗,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤缔刹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后劣针,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體校镐,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年捺典,在試婚紗的時候發(fā)現(xiàn)自己被綠了鸟廓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖肝箱,靈堂內(nèi)的尸體忽然破棺而出哄褒,到底是詐尸還是另有隱情,我是刑警寧澤煌张,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布呐赡,位于F島的核電站,受9級特大地震影響骏融,放射性物質(zhì)發(fā)生泄漏链嘀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一档玻、第九天 我趴在偏房一處隱蔽的房頂上張望怀泊。 院中可真熱鬧,春花似錦误趴、人聲如沸霹琼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枣申。三九已至,卻和暖如春看杭,著一層夾襖步出監(jiān)牢的瞬間忠藤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工楼雹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留模孩,地道東北人。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓贮缅,卻偏偏與公主長得像榨咐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子谴供,可洞房花燭夜當晚...
    茶點故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容