android - 尽管有更新,但应用内更新无法正常工作
问题描述
我正在尝试在我的应用中实现应用内更新,以便在有新更新时,用户可以看到它。
我已仔细遵循此处列出的所有步骤 - https://developer.android.com/guide/playcore/in-app-updates/kotlin-java
但由于某些原因,更新对话框没有显示...
活动.kt
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_DirectMusicPlayer)
super.onCreate(savedInstanceState)
initViews()
setContentView(binding.root)
}
private fun initViews(){
checkForUpdate()
}
private fun checkForUpdate(){
appUpdateManager = AppUpdateManagerFactory.create(this)
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
{
// Request the update.
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.FLEXIBLE' for flexible updates.
AppUpdateType.FLEXIBLE,
// The current activity making the update request.
this,
// Include a request code to later monitor this update request.
Constants.MY_REQUEST_CODE
)
}
else{
Toast.makeText(this, "No update yet", Toast.LENGTH_LONG).show()
}
}
//Create a listener to track request state updates.
installStateUpdatedListener = InstallStateUpdatedListener { state ->
when (state.installStatus()) {
InstallStatus.DOWNLOADING -> {
val bytesDownloaded = state.bytesDownloaded()
val totalBytesToDownload = state.totalBytesToDownload()
//show download progress
}
InstallStatus.DOWNLOADED -> {
//show SnackBar
showInstallSnackBar()
}
InstallStatus.INSTALLED -> {
appUpdateManager.unregisterListener(installStateUpdatedListener)
}
else -> {
//do something
}
}
}
// Before starting an update, register a listener for updates.
appUpdateManager.registerListener(installStateUpdatedListener)
}
private fun showInstallSnackBar(){
Snackbar.make(
findViewById(android.R.id.content),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE
).apply {
setAction("RESTART") { appUpdateManager.completeUpdate() }
setActionTextColor(ContextCompat.getColor(this@PermissionActivity, R.color.app_name_color))
show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == Constants.MY_REQUEST_CODE) {
if (resultCode != AppCompatActivity.RESULT_OK) {
// If the update is cancelled or fails,
// you can request to start the update again
checkForUpdate()
}
}
}
override fun onResume() {
super.onResume()
appUpdateManager
.appUpdateInfo
.addOnSuccessListener { appUpdateInfo ->
// If the update is downloaded but not installed,
// notify the user to complete the update.
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
showInstallSnackBar()
}
}
}
}
去测试:
- 我上传到测试轨道(使用应用内更新代码)
- 然后,我使用可共享链接下载了上面相同的应用程序包
- 然后在我的代码中,我将版本代码增加到比第 1步中的版本代码更高的版本代码
- 然后我生成了另一个签名包并上传到相同的内部测试轨道。
- 现在我在步骤 4中复制了应用程序的 URL并访问了...在那里我看到了“更新”——这意味着有可用的更新。(我没有点击 Playstore 上的更新)
- 我关闭了 PlayStore 并打开了我的应用程序...认为会显示一个应用程序更新对话框,但没有显示任何内容...但是在 Playstore 上,它显示为“更新”。
我也清除了缓存,等了几个小时,但什么也没有。
当没有更新时,我也没有收到我设置为“无更新”的 Toast 消息……这表明肯定有更新……但 Google 的更新对话框没有显示。
解决方案
推荐阅读
- javascript - ngx-editor 链接重定向到与 Angular 项目 url 结合的 url
- python-3.x - Keras 中的 model.evaluate 如何工作以及如何手动重新创建它?
- reactjs - 在另一个组件中输入,目标错误
- typescript - 我可以在不丢失类型检查的情况下迭代属性列表吗?
- macos - lldb 在 EXC_SYSCALL 上影响 rcx 值
- ios - CollectionView 不会停止抱怨未定义的行为
- c# - 另一个用户控件内的用户控件中的数据绑定
- sql-server - SQL Management Studio 中的数据库在服务器 A 中不可用,但在具有相同用户的服务器 B 中我可以看到所有数据库
- python - 如何在python的张量流中将成本函数定义为均方根对数误差
- jmeter - Jmeter 值映射