Home的好基友啊哈哈

作者: 卡路fly | 来源:发表于2017-08-15 20:00 被阅读61次

Home按键手势监听

home 按键监听需要打开广播进行监听,同时需要使用动态注册。


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()方法不一定会被调用,此时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.

当你想监听用户在正在运行的activity上进行了其他操作的时候,执行此方法。和onUserLeaveHint()配合进行状态栏通知的管理,他们是一对好基友。确保你的activity将获取到相关用户活动,例如下拉通知点击。触摸将调用此回调,touch-moved 和touch-up不会调用此回调。


相关文章

  • Home的好基友啊哈哈

    Home按键手势监听 home 按键监听需要打开广播进行监听,同时需要使用动态注册。 分别进行广播的注册和注销,在...

  • 临摹中超级喜欢的一幅图

    哈哈哈,好基友一起走,超级喜欢的漫画卡哇伊恶魔。

  • 半不邋遢,小道名也!

    每次想起也总因为担心基友,所以跑去传销组织…… 结果 基友在传销组织里泡妹子…… 哈哈哈 心疼~ 哈哈哈~ 下周大...

  • 当年我们看的电视剧有多乱,你真的知道吗?

    图解宗保的混乱亲戚史:我姑姑的男盆友的好基友是郭靖,我五叔的好基友是郭靖,我的好基友是郭靖,我爹的好基友还是郭靖,...

  • 好基友

    不知不觉都三年了,老大都快三岁了还没有个女盆友,老二也快两岁了,也没有女盆友,结果就这么的,它俩一到发情期就……互...

  • 好基友

  • 好基友

    “过去一年我很焦虑.....” “看到这段话我由衷的为你开心,之前聊天打电话我可以感觉出来你一直是比较苦闷的,我也...

  • “好基友”

    青春里会有那么几个人与你如影随形,不离不弃,一起奋斗,寻找梦想的轨迹。而那几个人就叫做“基友团”不一样的基友会给你...

  • 好基友

    好基友,一辈子。 每当我看到这句话的时候,就会想起一位高中同学。 昨天我还想起这位同学,心想要不要在网上发个寻人帖...

  • 好基友

    唐代诗人众多,李白杜甫如果说是塑料友情的话。那么元稹和白居易就真的是好基友了,铁哥们儿! 今天这首诗是白居易写给元...

网友评论

    本文标题:Home的好基友啊哈哈

    本文链接:https://www.haomeiwen.com/subject/jjphrxtx.html