Android Home键监听

  1. Home键是一个系统的按钮,我们无法通过onKeyDown进行拦截,它是拦截不到的,我们只能得到他在什么时候被按下了。就是通过广播接收者

    public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver { static final String SYSTEM_REASON = "reason"; static final String SYSTEM_HOME_KEY = "homekey"; static final String SYSTEM_RECENT_APPS = "recentapps"; @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处理点 } else if (reason.equals(SYSTEM_RECENT_APPS)) { // long home key处理点 } } } } }
  2. Activity中去注册这个广播接收者
    receiver = new HomeKeyEventBroadCastReceiver();
    registerReceiver(receiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
    
  3. Activity销毁的方法中去取消注册
    unRegisterReceiver(receiver);
    

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程