为什么应用内更新没有按预期更新应用?

Why is In-app updates doesn't update the app as expected?

以下是我的 SplashActivity.kt 中的代码。每当有可用的更新时,大多数情况下它根本不会显示,但我认为这不是因为我的代码中有任何错误,而是因为我了解到需要一些时间才能向用户展示新的更新。我现在担心的是,当它显示应用程序内更新屏幕时,它会关闭并且应用程序继续到下一个屏幕并且它不会要求用户更新应用程序以继续使用该应用程序。我弄错了吗?

    @Suppress("DEPRECATION")
class SplashActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySplashBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)

        callInAppUpdate()
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        Handler().postDelayed(
            {
                val currentUserID = FirestoreClass().getCurrentUserID()

                if (currentUserID.isNotEmpty()) {
                    startActivity(Intent(this@SplashActivity, HomeActivity::class.java))
                } else {
                    startActivity(
                        Intent(
                            this@SplashActivity,
                            LoginActivity::class.java
                        )
                    )
                }
                finish()
            },
            2500
        )
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (data == null) return

        if (requestCode == updateRequestCode) {
            Toast.makeText(this, "Downloading", Toast.LENGTH_SHORT).show()

            if (resultCode != RESULT_OK) {
                Log.d("TAG", "update flow failed $resultCode")
            }
        }
    }

    private val updateRequestCode = 1612

    private fun callInAppUpdate() {

        val appUpdateManager = AppUpdateManagerFactory.create(this)
        val appUpdateInfoTask = appUpdateManager.appUpdateInfo
        appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
            ) {
                appUpdateManager.startUpdateFlowForResult(
                    appUpdateInfo, AppUpdateType.IMMEDIATE, this, updateRequestCode
                )
            }
        }

    }

}

编辑:

我将 postDelayed 移到了 addOnSuccessListenerfun callInAppUpdate() 现在看起来像下面这样。

    private fun callInAppUpdate() {

    val appUpdateManager = AppUpdateManagerFactory.create(this)
    val appUpdateInfoTask = appUpdateManager.appUpdateInfo
    appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
        if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
            && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
        ) {
            appUpdateManager.startUpdateFlowForResult(
                appUpdateInfo, AppUpdateType.IMMEDIATE, this, updateRequestCode
            )
        }else{
            Handler().postDelayed(
                {
                    val currentUserID = FirestoreClass().getCurrentUserID()

                    if (currentUserID.isNotEmpty()) {
                        startActivity(Intent(this@SplashActivity, DashboardActivity::class.java))
                    } else {
                        startActivity(
                            Intent(
                                this@SplashActivity,
                                LoginWithPhoneNumberActivity::class.java
                            )
                        )
                    }
                    finish()
                },
                2500
            )
        }
    }

}

您在 postDelayed 中有导航逻辑。您在 2500 毫秒后明确导航到新 activity。所以代码完全按照你写的做。

如果你想在检查更新后导航,那么你可能应该将导航逻辑移至 addOnSuccessListener,尽管它看起来也很像 hack。无论哪种方式,我都不会在初始屏幕中进行此检查,而是在一些更“静态”的屏幕中,这些屏幕没有定时器设置的导航

您可以实现如下行为:

class SplashActivity : AppCompatActivity() {

private val updateRequestCode = 1612

private lateinit var binding: ActivitySplashBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivitySplashBinding.inflate(layoutInflater)
    setContentView(binding.root)

    callInAppUpdate()
    window.setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN
    )

}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (data == null) return

    if (requestCode == updateRequestCode) {
        Toast.makeText(this, "Downloading", Toast.LENGTH_SHORT).show()
        if (resultCode != RESULT_OK) {
            Log.d("TAG", "update flow failed $resultCode")
            navigate()
        }
    }
}

private fun navigate(){
    Handler().postDelayed(
        {
            val currentUserID = FirestoreClass().getCurrentUserID()

            if (currentUserID.isNotEmpty()) {
                startActivity(Intent(this@SplashActivity, DashboardActivity::class.java))
            } else {
                startActivity(
                    Intent(
                        this@SplashActivity,
                        LoginWithPhoneNumberActivity::class.java
                    )
                )
            }
            finish()
        },
        2500
    )
}



private fun callInAppUpdate() {

    val appUpdateManager = AppUpdateManagerFactory.create(this)
    val appUpdateInfoTask = appUpdateManager.appUpdateInfo
    appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
        if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
            && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
            appUpdateManager.startUpdateFlowForResult(
                appUpdateInfo, AppUpdateType.IMMEDIATE, this, updateRequestCode
            )
        }
        else {
            navigate()
        }
    }

  }
}