Android Home鍵監聽
如何捕獲Home鍵事件,並作出相應處理呢?其實系統還是給我們發出廣播的,下面是封裝好了的監聽Home鍵的方法:
- /**
- * Home鍵監聽封裝
- *
- * @author way
- *
- */
- public class HomeWatcher {
- static final String TAG = "HomeWatcher";
- private Context mContext;
- private IntentFilter mFilter;
- private OnHomePressedListener mListener;
- private InnerRecevier mRecevier;
- // 回撥介面
- public interface OnHomePressedListener {
- public void onHomePressed();
- public void onHomeLongPressed();
- }
- public HomeWatcher(Context context) {
- mContext = context;
- mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
- }
- /**
- * 設定監聽
- *
- * @param listener
- */
- public void setOnHomePressedListener(OnHomePressedListener listener) {
- mListener = listener;
- mRecevier = new InnerRecevier();
- }
- /**
- * 開始監聽,註冊廣播
- */
- public void startWatch() {
- if (mRecevier != null) {
- mContext.registerReceiver(mRecevier, mFilter);
- }
- }
- /**
- * 停止監聽,登出廣播
- */
- public void stopWatch() {
- if (mRecevier != null) {
- mContext.unregisterReceiver(mRecevier);
- }
- }
- /**
- * 廣播接收者
- */
- class InnerRecevier extends BroadcastReceiver {
- final String SYSTEM_DIALOG_REASON_KEY = "reason";
- final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
- final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
- final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
- @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_DIALOG_REASON_KEY);
- if (reason != null) {
- Log.e(TAG, "action:" + action + ",reason:" + reason);
- if (mListener != null) {
- if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
- // 短按home鍵
- mListener.onHomePressed();
- } else if (reason
- .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
- // 長按home鍵
- mListener.onHomeLongPressed();
- }
- }
- }
- }
- }
- }
- }
-
需要在應用中開啟監聽,下面是以在Activity中的應用為例:
- /**
- * 應用HomeWatcher小例
- *
- * @author way
- *
- */
- public class MainActivity extends Activity {
- private static final String TAG = "MainActivity";
- private HomeWatcher mHomeWatcher;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mHomeWatcher = new HomeWatcher(this);
- mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
- @Override
- public void onHomePressed() {
- Log.e(TAG, "onHomePressed");
- }
- @Override
- public void onHomeLongPressed() {
- Log.e(TAG, "onHomeLongPressed");
- }
- });
- }
- @Override
- protected void onResume() {
- super.onPause();
- mHomeWatcher.startWatch();// 在onResume中開始監聽,
- }
- @Override
- protected void onPause() {
- super.onPause();
- mHomeWatcher.stopWatch();// 在onPause中停止監聽,不然會報錯的。(實際登出廣播)
- }
- }
相關文章
- Android Home鍵、鎖屏鍵監聽Android
- Android 監聽home鍵的動作Android
- home鍵監聽(Android應用切換到後臺監聽)Android
- AndroidTV開發中所有的遙控器按鍵監聽及注意事項,新增home鍵監聽Android
- Android截圖監聽Android
- Android Hook 全面入侵監聽器AndroidHook
- Android.GridView事件監聽AndroidView事件
- Android 監聽生命週期工具庫Android
- Android掃碼槍監聽封裝Android封裝
- Android 監聽鍵盤彈出收起Android
- Android 解除安裝監聽詳解Android
- android PopupWindow監聽返回鍵無效Android
- 監聽 watch props物件屬性監聽 或深度監聽物件
- Android Service 服務的應用之電話監聽器以及簡訊監聽器Android
- Android的事件處理——監聽介面方式Android事件
- android監聽TF卡插拔事件 判斷Android事件
- 動態監聽與靜態監聽
- 動態監聽和靜態監聽
- 【監聽】兩庫互配靜態監聽
- ORACLE動態監聽與靜態監聽Oracle
- oracle靜態監聽和動態監聽Oracle
- 【oracle】動態監聽與靜態監聽Oracle
- Android搖一搖、螢幕方向的監聽Android
- Android 監聽鍵盤彈起及隱藏Android
- Android監聽軟鍵盤收起與彈出Android
- Android開機和關機廣播監聽Android
- android之Notification監聽系統清除通知欄Android
- Android studio(建立、監聽器intent選單)AndroidIntent
- 事件監聽事件
- Oracle 監聽Oracle
- SESSION監聽Session
- swift 訊息監聽和鍵值監聽(kvo)Swift
- H5頁面監聽Android物理返回鍵H5Android
- Android監聽使用者行為操作(AccessibilityService)Android
- 2018.03.31、Android-ObjectBox-監聽AndroidObject
- 監聽Android軟體盤彈出及隱藏Android
- Android開屏、鎖屏、解鎖監聽實現Android
- 同時配置動態監聽與靜態監聽