简体   繁体   English

当活动onPause()时显示警报对话框

[英]Display Alert Dialog when Activity onPause()

I am trying to show an alert dialog when a countdown timer finishes and the activity is sent in the background via the onPause() method. 我试图在倒数计时器结束并且通过onPause()方法在后台发送活动时显示警报对话框。

I did some research but couldn't find something to fit my needs. 我做了一些研究,但找不到适合我需要的东西。

I tried to achieve this by using a handler: 我试图通过使用处理程序来实现这一点:

private Handler myHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int aResponse = msg.getData().getInt("message");
                if (aResponse == DISPLAY_DLG) {
                    showStopAlarmDialog();
                }
            }
        };

And in the timer's onFinish() method 在计时器的onFinish()方法中

Message msgObj = myHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("message", DISPLAY_DLG);
msgObj.setData(b);
myHandler.sendMessage(msgObj);

However, this still only works when the activity is in the front. 但是,这仅在活动处于最前面时才起作用。

I would recommend using the AlarmManager for this task. 我建议为该任务使用AlarmManager Otherwise you have to be aware of handling a wake lock so the device doesn't goes to sleep, and if the time to elapse is too long then you will hit unnecessary battery drain. 否则,您必须知道要处理唤醒锁,以使设备不会进入睡眠状态;如果经过的时间过长,则会浪费不必要的电池。

Using the AlarmManager to fire an Intent and receive it in your app is the way that has work the better for me. 使用AlarmManager触发Intent并在您的应用程序中接收它对我来说是更好的方法。 Be sure to set the alarm when the activity goes to onPause() and unset the alarm when onResume() . 请确保在活动进入onPause()时设置警报,而在onResume()时取消设置警报。 You can also check if launching an Activity with Theme.Dialog is gonna suit better to your needs, 'cause if you lose the activity Context you won't be able to display a regular dialog. 您还可以检查是否启动带有Theme.Dialog的Activity是否更适合您的需求,因为如果丢失Activity Context,将无法显示常规对话框。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当对话框显示为活动时,OnStop()vs OnPause() - OnStop() vs OnPause() when dialog is displayed as a activity 从暂停中关闭警报对话框构建器 - Dismiss alert dialog builder from onpause 当活动为onPause()时启动一个timerTask - start a timerTask when activity is onPause() 即使应用程序未运行,也会显示警报对话框 - Alert dialog display even when the application is not running 用非活动类编写的警报对话框在从调用活动中解除时不显示警报对话框 - alert dialog written in non activity class not showing alert dialog when dismissed from calling activity 用户恢复应用程序后如何在活动中显示警报对话框 - How to display alert dialog in activity after user resume app 无法在 NoBar.FullScreen 活动中显示材料警报对话框 - Unable to display material alert dialog in NoBar.FullScreen activity 如何在Android应用程序的任何活动中显示警报对话框? - How to display alert dialog in any activity in android application? 活动未等待警报对话框 - Activity is not waiting for alert dialog 当要关闭Dialog时,为什么我们先调用活动方法onDestroy()然后onCreate()然后onPause()方法? - Why do we call activity method onDestroy() then onCreate() then onPause() methods when Dialog is to be dismissed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM