Warm tip: This article is reproduced from stackoverflow.com, please click
android google-play java

The android in-app update API is not working as I expected

发布于 2020-04-11 11:53:31
    @Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    appUpdateManager = AppUpdateManagerFactory.create(this);
    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(IMMEDIATE)) {
            // Request the update.
            try {
                appUpdateManager.startUpdateFlowForResult(
                        // Pass the intent that is returned by 'getAppUpdateInfo()'.
                        appUpdateInfo,
                        // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                        IMMEDIATE,
                        // The current activity making the update request.
                        this,
                        // Include a request code to later monitor this update request.
                        MY_REQUEST_CODE);

            }


            catch(IntentSender.SendIntentException e){
                e.printStackTrace();
            }


        }

    });

}

 @Override
protected void onResume() {
    super.onResume();

    appUpdateManager
            .getAppUpdateInfo()
            .addOnSuccessListener(
                    appUpdateInfo -> {
                        if (appUpdateInfo.updateAvailability()
                                == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                            try {
                                appUpdateManager.startUpdateFlowForResult(
                                        // Pass the intent that is returned by 'getAppUpdateInfo()'.
                                        appUpdateInfo,
                                        // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                                        IMMEDIATE,
                                        // The current activity making the update request.
                                        this,
                                        // Include a request code to later monitor this update request.
                                        MY_REQUEST_CODE);

                            }


                            catch(IntentSender.SendIntentException e){
                                e.printStackTrace();
                            }
                        }
                    });
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == MY_REQUEST_CODE) {
        if (resultCode != RESULT_OK) {
            Log.e("Main", "onActivityResult: app download failed");
        }
    }
}

This is the entire code from my MainActivity. I expected this to work as is as it comes right from the android docs. But it doesn't seem to do anything. To test it I put out a quick update and waited until it was approved. I opened it hoping to be blocked by a full screen saying I must update, but I could use the app freely.

Questioner
NickS
Viewed
136
NickS 2020-02-03 23:16

This actually just randomly worked about 36 hours later when I opened the app again. There must be a 24 hour delay or so from when update goes live til when the API will pick it up.