在 Android 上,如何检测显示系统对话框(电源选项、最近的应用程序、低电量...)?

时间:2023-04-23
本文介绍了在 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 上,如何检测显示系统对话框(电源选项、最近的应用程序、低电量...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:覆盖对话框 onBackPressed()? 下一篇:Android:如何从警报对话框启动 Activity

相关文章