欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android Home键监听的实现代码

程序员文章站 2024-03-03 20:57:04
之前的一个项目需要监听home键的事件,发现用dispatchkeyevent无法实现监听,后来查了些资料,才知道home键不能用常规的按键事件监听,需要使用广播的方法。下...

之前的一个项目需要监听home键的事件,发现用dispatchkeyevent无法实现监听,后来查了些资料,才知道home键不能用常规的按键事件监听,需要使用广播的方法。下面是我封装的一个工具类。根据自己实际需要修改吧。

注册广播:

homelistener.getinstance().start(this);

移除广播

homelistener.getinstance().stop(this);

设置监听回调

homelistener.getinstance().sethomekeylistener(homepresslistener listener);

添加监听回调

homelistener.getinstance().addhomekeylistener(homepresslistener listener)

移除监听回调

homelistener.getinstance().removehomekeylistener(homepresslistener listener);

移除所有监听回调

homelistener.getinstance().removeallhomekeylistener();

销毁

homelistener.getinstance().destroy();

工具类代码:

import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;

import java.util.arraylist;
import java.util.list;
import java.util.concurrent.atomic.atomicboolean;

public class homelistener {
 public static homekeylistener getinstance() {
  homekeylistener listener = homekeylistener.slistener;
  listener.init();
  return listener;
 }

 static class homekeylistener {
  private static final string system_dialog_reason_recent_apps = "recentapps";//home键旁边的最近程序列表键
  private static final string system_dialog_reason_home_key = "homekey";//按下home键
  private static final string system_dialog_reason_assist = "assist";//某些三星手机的程序列表键

  private static atomicboolean isdestroy = new atomicboolean(true);
  private static atomicboolean isregister = new atomicboolean(false);
  private static homekeylistener slistener = new homekeylistener();
  private list<homepresslistener> mpresslisteners = new arraylist<homepresslistener>();
  private homereceiver mreceiver;
  private intentfilter mhomefileter;
  private homepresslistener mhomepresslistener;
  public void init() {
   if (isdestroy.get()) {
    this.mpresslisteners = new arraylist<homepresslistener>();
    this.mreceiver = new homereceiver();
    this.mhomefileter = new intentfilter(intent.action_close_system_dialogs);
    isdestroy.set(false);
   }
  }

  public synchronized void start(context context) {
   if (!isregister.get()) {
    context.registerreceiver(mreceiver, mhomefileter);
    isregister.set(true);
   }
  }

  public synchronized void stop(context context) {
   if (isregister.get()) {
    context.unregisterreceiver(mreceiver);
    isregister.set(false);
   }
  }

  public void sethomekeylistener(homepresslistener listener) {
   this.mhomepresslistener = listener;
  }

  public void addhomekeylistener(homepresslistener listener) {
   mpresslisteners.add(listener);
  }

  public void removehomekeylistener(homepresslistener listener) {
   mpresslisteners.add(listener);
  }

  public void removeallhomekeylistener() {
   mpresslisteners.clear();
  }

  public void destroy() {
   this.mpresslisteners.clear();
   this.mpresslisteners = null;
   this.mreceiver = null;
   this.mhomefileter = null;
   this.mhomepresslistener = null;
   isdestroy.set(true);
  }

  public interface homepresslistener {
   void onhomepress();
   void onhomerecentappspress();
  }

  class homereceiver extends broadcastreceiver {
   @override
   public void onreceive(context context, intent intent) {
    receive(intent);
   }
  }

  private void receive(intent intent) {
   string action = intent.getaction();
   if (action != null && action.equals(intent.action_close_system_dialogs)) {
    string reason = intent.getstringextra( "reason" );
    dispatchpress(reason);
   }
  }

  private void dispatchpress(string reason) {
   switch (reason) {
    case system_dialog_reason_home_key:
     if (mhomepresslistener != null) mhomepresslistener.onhomepress();
     for (homepresslistener listener : mpresslisteners) listener.onhomepress();
     break;

    case system_dialog_reason_recent_apps:
    case system_dialog_reason_assist:
     if (mhomepresslistener != null) mhomepresslistener.onhomerecentappspress();
     for (homepresslistener listener : mpresslisteners) listener.onhomerecentappspress();
     break;

   }
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。