Ibr*_*zin 8 android kotlin google-play android-studio
我正在尝试在我的应用程序中实现应用程序内更新,以便当有新的更新时,用户可以看到它。
我已仔细遵循此处列出的所有步骤 - 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()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
去测试:
我还清除了缓存,等了几个小时但什么也没发生。
当没有更新时,我也没有收到设置为“无更新”的 Toast 消息...这表明肯定有更新...但来自 Google 的更新对话框未显示。
归档时间: |
|
查看次数: |
1715 次 |
最近记录: |