防止在重新运行应用程序后关闭对话框



我正在实现一个对话框函数,有一个问题。

我想防止我的对话框在一段时间内关闭。例如,如果我将其设置为10分钟,则尽管用户在10分钟内终止并重新运行应用程序,对话框仍应再次出现。10分钟后,它将被驳回,用户最终可以使用该应用程序。如何将对话框设置为在特定时间段内不关闭?

我已经尝试了如下所示的代码

我通过用sharedPreference存储开始时间,然后开始时间-当前时间>600000(10分钟(

Xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_information_button">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_search_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.Icon"
android:id="@+id/btn_go_search"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_marginStart="10dp"
android:backgroundTint="#2E6DC6"
android:text="Next"
android:textColor="@color/white"
android:textSize="@dimen/text_size_15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@+id/edit_search"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edit_search"
android:layout_width="0dp"
android:layout_height="55dp"
android:background="@drawable/bio_email_search_box"
android:hint="@string/biometric_change_email_hint"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="6"
android:padding="10dp"
android:selectAllOnFocus="true"
android:textColor="@color/white"
android:textSize="@dimen/text_size_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_go_search"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

片段

private lateinit var binding : FragmentHelpBinding
private lateinit var viewModel : HelpViewModel
private lateinit var mContext: MainActivity
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context as MainActivity
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_help, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this).get(HelpViewModel::class.java)
binding.viewModel = viewModel
otpTimeLimit()
}
private fun otpTimeLimit() = runBlocking {
val currentTime = System.currentTimeMillis()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var startTime = currentTime
var pref: SharedPreferences? = requireContext().getSharedPreferences("otpTrialLimit", MODE_PRIVATE)
pref?.edit()?.putLong("startTime", startTime)
pref?.edit()?.commit()

if (System.currentTimeMillis() - startTime <600000) {
// dialog will show up here and should last for 10 min 
clickDialog()
}else if (System.currentTimeMillis() - startTime >=600000) {
// and when 10 minutes passed, it should dismiss              
}
}

private fun clickDialog() {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.help_dialog, null)
var alertDialog = AlertDialog.Builder(requireContext(), R.style.CustomAlertDialog)
.setView(view)
.create()
val titleText : TextView = view.findViewById(R.id.title_text_dialog)
val contentText : TextView = view.findViewById(R.id.content_text_dialog)
binding.btnGoSearch.setOnClickListener {
titleText.text = getString(R.string.help_notification)
contentText.text = getString(R.string.notification_content)
alertDialog.show()
}
}

我的建议是,当用户看到对话框时,您应该存储当前日期/时间,如果用户关闭并打开应用程序,则获取当前日期/日期,并将其与存储的日期/时间进行比较。

不要忘记使用:setCancelable(false)来防止用户关闭对话框

最新更新