I want to implement Android In-App Update, but the code tells me that the call to onActivityResult
is deprecated.
I have tried the suggested in this answer to solve it.
This is my code into MainActivity
:
private val updateFlowResultLauncher =
registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult(),
) { result ->
if (result.resultCode == RESULT_OK) {
// Handle successful app update
}
}
private fun checkAppUpdate() {
val starter =
IntentSenderForResultStarter { intent, _, fillInIntent, flagsMask, flagsValues, _, _ ->
val request = IntentSenderRequest.Builder(intent)
.setFillInIntent(fillInIntent)
.setFlags(flagsValues, flagsMask)
.build()
updateFlowResultLauncher.launch(request)
}
appUpdateManager = AppUpdateManagerFactory.create(this)
appUpdateManager.registerListener(installStateUpdatedListener)
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo: AppUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)
) {
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.FLEXIBLE,
starter,
UPDATE_REQUEST_CODE
)
} catch (e: SendIntentException) {
e.printStackTrace()
}
}
}
}
private fun checkNewAppVersionState() {
appUpdateManager
.appUpdateInfo
.addOnSuccessListener { appUpdateInfo: AppUpdateInfo ->
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
popupAlerter()
}
}
}
private fun popupAlerter() {
val snackbar = Snackbar.make(
findViewById(android.R.id.content),
"Nueva versión descargada, por favor reinicia",
Snackbar.LENGTH_INDEFINITE
)
snackbar.setAction("REINICIAR") { view: View? -> appUpdateManager.completeUpdate() }
snackbar.setActionTextColor(
ContextCompat.getColor(this, R.color.colorAccent)
)
snackbar.show()
}
private fun unregisterListener() {
appUpdateManager.unregisterListener(
installStateUpdatedListener
)
}
I have uploaded an internal test with a version older than the version published in the Play Store, but it does not work.
What is wrong in my code?