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

问题描述 投票:1回答:1

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

  1. 如何记录主机(始终处于活动状态)或正在结束时的显示时间?

  2. 如果需要,其他主机恢复后如何重新显示对话框?

我通过以下代码尝试了,但是没用

// 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中的全局对话框?感谢您的收看和回答:)

android android-alertdialog android-application-class
1个回答
0
投票
根据您提供的示例,创建一个私有静态变量以节省时间并在显示对话框时更新计时器,并且在活动的[[onResume()中,您可以调用

resumeToShow()并检查时间,并根据需要显示对话框

© www.soinside.com 2019 - 2024. All rights reserved.