最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Home鍵監(jiān)聽的實(shí)現(xiàn)代碼

 更新時(shí)間:2018年12月23日 09:26:04   作者:Mr_MaChao  
這篇文章主要介紹了Android Home 鍵監(jiān)聽的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

之前的一個(gè)項(xiàng)目需要監(jiān)聽home鍵的事件,發(fā)現(xiàn)用dispatchKeyEvent無法實(shí)現(xiàn)監(jiān)聽,后來查了些資料,才知道home鍵不能用常規(guī)的按鍵事件監(jiān)聽,需要使用廣播的方法。下面是我封裝的一個(gè)工具類。根據(jù)自己實(shí)際需要修改吧。

注冊(cè)廣播:

HomeListener.getInstance().start(this);

移除廣播

HomeListener.getInstance().stop(this);

設(shè)置監(jiān)聽回調(diào)

HomeListener.getInstance().setHomeKeylistener(HomePressListener listener);

添加監(jiān)聽回調(diào)

HomeListener.getInstance().addHomeKeyListener(HomePressListener listener)

移除監(jiān)聽回調(diào)

HomeListener.getInstance().removeHomeKeyListener(HomePressListener listener);

移除所有監(jiān)聽回調(diào)

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";//某些三星手機(jī)的程序列表鍵

  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;

   }
  }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

华安县| 周口市| 深水埗区| 子洲县| 甘孜| 绍兴县| 门源| 旬邑县| 沙坪坝区| 四川省| 平湖市| 禄劝| 临洮县| 枣庄市| 岳西县| 临高县| 兴城市| 驻马店市| 龙井市| 元阳县| 临沂市| 客服| 临西县| 湘潭市| 化隆| 沁源县| 昌吉市| 长顺县| 文化| 北宁市| 耿马| 河北省| 航空| 安平县| 定日县| 贡嘎县| 内乡县| 余干县| 义马市| 民乐县| 越西县|