On Android, how to detect a system dialog is displayed (power options, recent apps, low battery...)?(在 Android 上,如何检测显示系统对话框(电源选项、最近的应用程序、低电量...)?)
问题描述
我想捕捉任何导致我的活动显示甚至部分隐藏的东西,例如电源选项、最近的应用程序托盘、低电量通知等...我很难检测到这些系统事件.
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.
我很确定当此类事件发生时会调用 onPause(),但它似乎是错误的......还是我?
I was pretty sure onPause() would be called when such events happen, but it seems to be wrong... or is it me?
还有其他想法吗?...我最好不要单独挂钩每个系统广播操作,因为我希望尽可能通用(并对隐藏我的 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).
推荐答案
在开发一个 kiosk 风格的应用程序时,我知道一些 Dialogs 会出现在前台并且可以被检测到
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.
一个反例是没有出现在前台的电源按钮对话框(关闭、重启等).
A counter-example is the power-button dialog (Turn off, Reboot etc) that does not come to the foreground.
请注意,您可以使用此广播关闭系统对话框(甚至是电源按钮对话框):
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);
但在大多数(所有较新的?)设备上,此广播甚至会关闭软件键盘,因此不建议运行频繁发送它的服务,因为用户将无法在文本字段中输入任何内容.
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 上发布.
Note that such behaviour will definetly gratify your app a status as beeing malware, keeping it from beeing published on google play.
这篇关于在 Android 上,如何检测显示系统对话框(电源选项、最近的应用程序、低电量...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android 上,如何检测显示系统对话框(电源选项、最近的应用程序、低电量...)?
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
