有没有办法在android中显示全局对话框?;

有没有办法在android中显示全局对话框?;,android,android-alertdialog,android-application-class,Android,Android Alertdialog,Android Application Class,我需要在Android应用程序上显示并维护一个对话框,该对话框在一段时间(例如:30秒)后消失。但我遇到了一些困难: 主机(始终处于活动状态)被破坏或正在完成时,如何记录显示时间 如果需要,如何在其他主机恢复时重新显示对话框 我用下面的代码进行了尝试,但它不起作用 // dialog that I want to show. class BusinessBroadcastDialog(activity: Activity, private val tag: String) : AlertDial

我需要在Android应用程序上显示并维护一个对话框,该对话框在一段时间(例如:30秒)后消失。但我遇到了一些困难:

  • 主机(始终处于活动状态)被破坏或正在完成时,如何记录显示时间

  • 如果需要,如何在其他主机恢复时重新显示对话框

  • 我用下面的代码进行了尝试,但它不起作用

    // dialog that I want to show.
    class BusinessBroadcastDialog(activity: Activity, private val tag: String) : AlertDialog(activity)
    
    object GlobalShowableManager {
    
        fun show(duration: Int) {
            // ActivityRecorder.get() will return the activity which is on the top of task.
            val activity = ActivityRecorder.get()
            if (activity?.isActivityExist() == true) {
                val tag = "${activity.javaClass.simpleName}#BusinessBroadcastDialog#$duration"
                val dialog = BusinessBroadcastDialog(activity, tag).apply {
                    ownerActivity = activity
                }
                dialog.show()
            } else {
                Log.i(TAG, "no exist activity to show global dialog, break.")
            }
        }
    
        fun resumeToShow() {
            // will be called when other host resumed.
            // get last BusinessBroadcastDialog showing time mark it as t. 
            // if t >= duration then do nothing, 
            // else let t = t - duration and show dialog. dialog will disappear after t seconds.
        }
    }
    
    

    有没有更好的方式在android中显示全局对话框?感谢您的观看和回答:)

    实现全局对话框的最简单方法是使用一个活动和多个片段体系结构,然后每个片段都是新屏幕,您可以使用活动控制对话框


    根据您给出的示例,创建一个私有静态变量以节省时间,并在显示对话框时更新计时器。在活动的onResume()中,您可以调用resumethow()并检查时间,如果需要或不需要,显示对话框

    您可以使用
    android.arch.lifecycle
    在appresumed()和onAppStopped()上获取应用程序生命周期。与倒计时一起,您可以显示对话框,但当应用程序停止时,您可以取消countDownTimer@Shadow谢谢你的回答,你的回答救了我很多。最后,我通过向所有存在的活动发出一个事件来解决这个问题,该事件记录显示时间和其他额外数据。当一个exist活动收到这个事件时,我向这个活动的rootView添加了一个视图(看起来像一个对话框),当显示时间变为零时,这个视图将消失。很有效,谢谢你的回答。我的Android应用程序有多个活动。所以我担心你的建议行不通。