温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - The android in-app update API is not working as I expected
android google-play java

java - Android应用程式内更新API无法正常运作

发布于 2020-04-11 14:03:33
    @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");
        }
    }
}

这是我的MainActivity中的全部代码。我期望它能像从android docs一样直接工作。但这似乎无能为力。为了测试它,我发布了一个快速更新并等待它被批准。我打开它是希望被全屏阻止,说我必须更新,但我可以自由使用该应用程序。

查看更多

提问者
NickS
被浏览
83
NickS 2020-02-03 23:16

实际上,大约36小时后,当我再次打开该应用程序时,它才随机工作。从更新开始生效直到API提取它,必须有24小时左右的延迟。