3

I saw this dialog always appears when I open old version of Tokopedia app. Tokopedia suggests me to update the app.

enter image description here

The dialog gives me two methods to update the app:

  • Update now
  • Update when Wi-Fi available

If I select Lain Kali (Cancel), the dialog appears again on the next app open. But, if I select the second option, I open Play Store and see this behavior:

enter image description here

It really do update on background until my device is connected to Wi-Fi.

I want to mimic the same action like Tokopedia did, because some version of my app contains critical bug. I want to give users a better user experience.

Do you know how to show the dialog above?

Anupam
  • 2,845
  • 2
  • 16
  • 30
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • 1
    It is better you can create a new web service in the first screen of your application. It will provide the latest version of the application in the play store and other required details. You have to compare installed application version with the version got in Web service response. Then you can show the dialog. – DAS Jun 27 '19 at 06:16
  • @SibinDavis I have a web service to detect whether users are using old version or not. Now the question is, how to show the dialog? And when users click Update, the dialog tells Play Store to update our app. I am sure that the dialog comes from Google Play Service API. – Anggrayudi H Jun 27 '19 at 06:20
  • No need of Google Play Service API. You can design a dialog like above and while clicking on the update button, you can redirect to your application's store page. And if they are clicking the cancel button, you can dismiss the dialog and redirect to next screen. – DAS Jun 27 '19 at 06:23

3 Answers3

3

This is possible using In-App Update provided by Google.

In-app updates works only with devices running Android 5.0 (API level 21) or higher, and requires you to use Play Core library 1.5.0 or higher. There are two types - 1.Flexible and 2. Immediate.

Follow this link and implement In-App Update as per your requirement.

https://developer.android.com/guide/app-bundle/in-app-updates

Anupam
  • 2,845
  • 2
  • 16
  • 30
1

You can achieve this by using Support in-app updates

  • It only work from Android 5.0 (API level 21) or higher.

  • There are two types of Update Available with UX for in-app updates:

    • Flexible
    • Immediate

To Check for update availability

// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context);

// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
          // For a flexible update, use AppUpdateType.FLEXIBLE
          && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
              // Request the update.
    }
});
Nikunj
  • 3,937
  • 19
  • 33
1

Okay, here's the complete code as requested by @akshay_shahane.

Firstly, add this line on your app's build.gradle:

dependencies {
    implementation 'com.google.android.play:core:1.6.1'
}

And inside your activity:

class MainActivity : AppCompatActivity(), InstallStateUpdatedListener {

    private val appUpdateManager by lazy {
        AppUpdateManagerFactory.create(this).also { it.registerListener(this) }
    }

    override fun onDestroy() {
        if (Build.VERSION.SDK_INT >= 21) {
            appUpdateManager.unregisterListener(this)
        }
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        if (Build.VERSION.SDK_INT >= 21) {
            appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
                // If the update is downloaded but not installed, notify the user to complete the update.
                if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                    popupSnackbarForCompleteUpdate()
                } else if (it.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS
                        && it.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    appUpdateManager.startUpdateFlowForResult(it, AppUpdateType.IMMEDIATE, this, REQUEST_CODE_UPDATE_APP)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == ActivityResult.RESULT_IN_APP_UPDATE_FAILED && requestCode == REQUEST_CODE_UPDATE_APP) {
            Toast.makeText(this, "Update failed", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onStateUpdate(state: InstallState) {
        when (state.installStatus()) {
            InstallStatus.DOWNLOADED -> popupSnackbarForCompleteUpdate()
            InstallStatus.REQUIRES_UI_INTENT -> {
                Snackbar.make(findViewById(R.id.activity_main_layout),
                        "To perform the installation, a Play Store UI flow needs to be started.",
                        Snackbar.LENGTH_LONG
                ).show()
            }
            else -> {
                val stateString = when (state.installStatus()) {
                    InstallStatus.FAILED -> "failed"
                    InstallStatus.PENDING -> "pending"
                    InstallStatus.DOWNLOADING -> "downloading"
                    InstallStatus.INSTALLING -> "installing"
                    InstallStatus.INSTALLED -> "installed"
                    InstallStatus.CANCELED -> "canceled"
                    else -> null
                }
                if (stateString != null) {
                    Snackbar.make(findViewById(R.id.activity_main_layout),
                            "An update is $stateString.",
                            Snackbar.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }

    private fun popupSnackbarForCompleteUpdate() {
        Snackbar.make(findViewById(R.id.activity_main_layout),
                "An update is ready to install.",
                Snackbar.LENGTH_INDEFINITE
        ).apply {
            setAction("INSTALL") { appUpdateManager.completeUpdate() }
            show()
        }
    }

    @RequiresApi(21)
    fun checkUpdateViaGooglePlay() {
        appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
            when (appUpdateInfo.updateAvailability()) {
                UpdateAvailability.UPDATE_AVAILABLE -> {
                    if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                        appUpdateManager.startUpdateFlowForResult(
                            appUpdateInfo, AppUpdateType.FLEXIBLE, this, REQUEST_CODE_UPDATE_APP)
                    } else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                        appUpdateManager.startUpdateFlowForResult(
                            appUpdateInfo, AppUpdateType.IMMEDIATE, this, REQUEST_CODE_UPDATE_APP)
                    }
                }
                UpdateAvailability.UPDATE_NOT_AVAILABLE -> {
                    Toast.makeText(this, R.string.no_updates_found, Toast.LENGTH_SHORT).show()
                }
            }
        }.addOnFailureListener {
            Toast.makeText(this, R.string.error_check_update, Toast.LENGTH_SHORT).show()
        }
    }

    companion object {
        const val REQUEST_CODE_UPDATE_APP = 8
    }
}
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87