在Android上,如何检测系统对话框(电源选项,最近的应用程序,电池电量不足...)? [英] On Android, how to detect a system dialog is displayed (power options, recent apps, low battery...)?

查看:127
本文介绍了在Android上,如何检测系统对话框(电源选项,最近的应用程序,电池电量不足...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Stackoverflowers(我知道,听起来不应该是...)



我想这几乎都是在这个问题:我想抓住任何导致我的活动显示甚至部分隐藏的东西,例如电源选项,最近的应用程序托盘,电池电量不足通知等,我很难检测到这些系统事件。



我很确定onPause( )会在这样的事件发生时被调用,但是似乎是错误的...或者是我吗?



任何其他想法?挂钩每个系统广播动作单独,因为我想尽可能的通用(并对任何隐藏我的活动的任何事情做出反应)。



感谢任何输入!

解决方案

在一个信息亭风格的应用程序上工作时,我知道一些对话框来到前台,可以通过

  ActivityManager activityManager =(ActivityManager)getBaseContext()
.getSystemService(Activity.ACTIVITY_SERVICE);
String className = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();

一个例子是将com.android.settings带到前台的蓝牙绑定对话框



一个反例是电源按钮对话框(关闭,重新启动等),不会出现在前景。



请注意,您可以关闭此广播的系统对话框(即使是电源按钮对话框):

 意图closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
sendBroadcast(closeDialog);但是在大多数(所有较新的)设备上,广播甚至会关闭软件键盘,所以它是不建议运行服务经常发送,因为用户将无法将任何内容输入到文本字段中。



请注意,这样的行为将绝对满足您的应用程序一个恶意软件的状态,保持它不会在谷歌播放发布。


Hi Stackoverflowers (I know, that doesn't sound like it should...)

Well I think It's almost all in the question: 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.

I was pretty sure onPause() would be called when such events happen, but it seems to be wrong... or is it me?

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).

Thanks for any input!

解决方案

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();

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.

Note that such behaviour will definetly gratify your app a status as beeing malware, keeping it from beeing published on google play.

这篇关于在Android上,如何检测系统对话框(电源选项,最近的应用程序,电池电量不足...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆