几天后用户再次拒绝更新时,如何显示应用内更新对话框?

How to show in-app update dialog when the user has denied the update previously after few days again?

我已经实现了在有可用更新时显示灵活的应用程序内更新对话框的功能。但是当用户选择不更新应用程序并重新启动应用程序时,对话框再次出现。在用户选择不安装更新几天后,如何再次显示该对话框?

您可以使用 Android 中的 SharedPreferences。基本上,当您要求灵活更新时,将日期保存到 SharedPreferences 并将当前日期与存储在共享首选项中的日期进行比较,只要用户使用该应用程序来决定何时要求下一次更新。

我建议您只询问一次,因为多次询问会惹恼您的用户。

 else if (checkVersion.android_current_version!! > versionName.toLong()) {
                    if (SharedPreference(this@WelcomeActivity).getInt(KEY_UPDATE_ASKED) != (checkVersion.android_current_version as Long).toInt()) {
                        appUpdateManager = AppUpdateManagerFactory.create(this@WelcomeActivity)
                        val appUpdateInfoTask = appUpdateManager!!.appUpdateInfo

                        appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
                            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                                appUpdateManager!!.startUpdateFlowForResult(
                                        appUpdateInfo,
                                        AppUpdateType.FLEXIBLE,
                                        this@WelcomeActivity,
                                        REQUEST_FLEXIBLE_UPDATE)
                            }
                        }
                    }
                }

这段代码(你需要根据自己的代码修改)让我只要checkVersion.android_current_version(我存储在Firebase中的版本所以我也可以决定隐藏FlexibleUpdates for某些版本)高于用户 phone 中安装的版本。您的用例要求您删除输入条件并检查日期差异,而不是检查之前是否询问过更新。

SharedPreferences

的助手class
class SharedPreference(val context: Context) {
    private val PREFS_NAME = "PREFERENCES"
    val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    fun save(KEY_NAME: String, value: String) {
        val editor: SharedPreferences.Editor = sharedPref.edit()

        editor.putString(KEY_NAME, value)

        editor.apply()
    }


    fun getString(KEY_NAME: String): String? {

        return sharedPref.getString(KEY_NAME, null)
    }
}