android 捕获Home键和ACTION_TIME_TICK广播
生活随笔
收集整理的這篇文章主要介紹了
android 捕获Home键和ACTION_TIME_TICK广播
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
捕獲方法說明
1.Intent.ACTION_CLOSE_SYSTEM_DIALOGS和Intent.ACTION_TIME_TICK不能再Manifest中注冊使用,必須在程序中動態注冊
2.Intent.ACTION_TIME_TICK按照時鐘刻度,即開始下一分鐘時自動發送廣播
3.這2種廣播屬于“程序”級別的廣播,不能作為app級別的廣播使用
二、代碼實戰
捕獲HomeKey與TIME_TICK
public?class?CapatureHomeKey?extends?BaseActivity?{HomeKeyEventBroadCastReceiver?receiver;@Overrideprotected?void?onCreate(Bundle?saveInstance)?{super.onCreate(saveInstance);getWindow().setBackgroundDrawableResource(R.drawable.avatar12);receiver?=?new?HomeKeyEventBroadCastReceiver();IntentFilter?filter=new?IntentFilter();filter.addAction(Intent.ACTION_TIME_TICK);filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);registerReceiver(receiver,filter);}@Overrideprotected?void?onDestroy()?{super.onDestroy();unregisterReceiver(receiver);}private?class?HomeKeyEventBroadCastReceiver?extends?BroadcastReceiver?{static?final?String?SYSTEM_REASON?=?"reason";static?final?String?SYSTEM_HOME_KEY?=?"homekey";//?home?keystatic?final?String?SYSTEM_RECENT_APPS?=?"recentapps";//?long?home?keyprivate?final?String?TAG?=?"HomeKeyTag";@Overridepublic?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?(SYSTEM_HOME_KEY.equals(reason))?{Log.d(TAG,?"--->"+SYSTEM_HOME_KEY);?//捕獲到Home鍵}else?if?(SYSTEM_RECENT_APPS.equals(reason))?{Log.d(TAG,?"--->"+SYSTEM_RECENT_APPS);?//捕獲到最近打開的Activity}}else?if?(action.equals(Intent.ACTION_TIME_TICK))?{//to?do}}}}轉載于:https://my.oschina.net/ososchina/blog/347925
總結
以上是生活随笔為你收集整理的android 捕获Home键和ACTION_TIME_TICK广播的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript常用对象
- 下一篇: 事务的隔离级别(Transaction