android平板Home键的监听

关于android4.2平板Home键的监听

1、android4.0以上的平板Home键是不能屏蔽的,至少在应用层是这样。但是可以监听Home键的触发,主要是通过广播的监听来接受Hone键的触发广播。在这里,有必要重点提醒一下,接收Home的点击广播之前,这个Home键该处理的事情已经做了,也就是说,已经回到Launcher桌面,回调了Onresume()方法。

2、具体代码实现:

// 监听home键广播,在oncreate()方法里面注册广播
        receiver = new HomeKeyEventBroadCastReceiver();
        registerReceiver(receiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
 
 
//广播接收器
class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
        static final String SYSTEM_REASON = "reason";
        static final String SYSTEM_HOME_KEY = "homekey";// home key
        static final String SYSTEM_RECENT_APPS = "recentapps";// long home key
         
        @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_REASON);
                if (reason != null) {
                    if (reason.equals(SYSTEM_HOME_KEY)) {
                        // home key处理点
                        pager.setCurrentItem(2);
                    }
                    else if (reason.equals(SYSTEM_RECENT_APPS)) {
                        // long homekey处理点
                    }
                }
            }
        }
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。