問題描述
我想捕捉任何導致我的活動顯示甚至部分隱藏的東西,例如電源選項、最近的應用程序托盤、低電量通知等...我很難檢測到這些系統(tǒng)事件.
I'd like to catch anything that causes the display of my Activity to get even partially hidden, e.g. power options, recent apps tray, low battery notification, etc... and I'm having a hard time to detect these system events.
我很確定當此類事件發(fā)生時會調(diào)用 onPause(),但它似乎是錯誤的......還是我?
I was pretty sure onPause() would be called when such events happen, but it seems to be wrong... or is it me?
還有其他想法嗎?...我最好不要單獨掛鉤每個系統(tǒng)廣播操作,因為我希望盡可能通用(并對隱藏我的 Activity 的任何事情做出反應).
Any other idea?... I'd preferably not hook on each system broadcast action individually, since I'd like to be as generic as possible (and react to ANYTHING that hides my Activity).
推薦答案
在開發(fā)一個 kiosk 風格的應用程序時,我知道一些 Dialogs 會出現(xiàn)在前臺并且可以被檢測到
On working on a kiosk style app I know that some Dialogs come to the foreground and can be detected by
ActivityManager activityManager = (ActivityManager)getBaseContext()
.getSystemService(Activity.ACTIVITY_SERVICE);
String className = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
一個例子是藍牙綁定對話框,它將 com.android.settings 帶到前臺.
An example for that is the bluetooth-binding dialog that brings the com.android.settings to the foreground.
一個反例是沒有出現(xiàn)在前臺的電源按鈕對話框(關閉、重啟等).
A counter-example is the power-button dialog (Turn off, Reboot etc) that does not come to the foreground.
請注意,您可以使用此廣播關閉系統(tǒng)對話框(甚至是電源按鈕對話框):
Note that you can close system dialogs (even the power-button dialog) with this broadcast:
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
但在大多數(shù)(所有較新的?)設備上,此廣播甚至會關閉軟件鍵盤,因此不建議運行頻繁發(fā)送它的服務,因為用戶將無法在文本字段中輸入任何內(nèi)容.
But on most (all newer?) devices this Broadcast will even close the software keyboard, so it is not advisable to have a service running that frequently sends it as the user will then be unable to enter anything into a text field.
請注意,此類行為肯定會使您的應用程序成為惡意軟件,使其無法在 google play 上發(fā)布.
Note that such behaviour will definetly gratify your app a status as beeing malware, keeping it from beeing published on google play.
這篇關于在 Android 上,如何檢測顯示系統(tǒng)對話框(電源選項、最近的應用程序、低電量...)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!