首页 > 解决方案 > Android:应用内更新:java - 无法看到应用内更新弹出窗口

问题描述

我尝试了此网址中的以下代码,但更新弹出窗口永远不会打开。我做了内部测试应用程序。我只收到敬酒消息直到Toast(On CheckUpdate method,...)

我基本上是在尝试向用户显示应用内更新(立即)弹出窗口以允许更新应用程序,但弹出窗口永远不会出现。是我写错了还是我遗漏了什么!您的帮助将不胜感激。

下面的代码有什么问题?

implementation 'com.google.android.play:core:1.10.0'

--

     AppUpdateManager appUpdateManager ;
    private int MY_REQUEST_CODE = 999;

    private void checkUpdate() {
        Toast.makeText(MainActivity.this, "On CheckUpdate method", Toast.LENGTH_LONG).show();
         appUpdateManager = AppUpdateManagerFactory.create(this);

// 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
                    // This example applies an immediate update. To apply a flexible update
                    // instead, pass in AppUpdateType.FLEXIBLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                // Request the update.
                Toast.makeText(MainActivity.this, "Request the update.", Toast.LENGTH_LONG).show();
                try {
                    appUpdateManager.startUpdateFlowForResult(
                            // Pass the intent that is returned by 'getAppUpdateInfo()'.
                            appUpdateInfo,
                            // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                            AppUpdateType.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) {
                    Toast.makeText(MainActivity.this, "check fail.."+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                Toast.makeText(MainActivity.this, "Update flow failed! Result code: ", Toast.LENGTH_LONG).show();
                Log.e("MainActivity","Update flow failed! Result code: " + resultCode);
                // If the update is cancelled or fails,
                // you can request to start the update again.
            }
            if (resultCode != RESULT_CANCELED) {
                Toast.makeText(MainActivity.this, "Update flow failed! Result code: " + resultCode, Toast.LENGTH_LONG).show();
                Log.e("MainActivity","Update flow failed! Result code: " + resultCode);
                // If the update is cancelled or fails,
                // you can request to start the update again.
            }
            if (resultCode != ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
                Toast.makeText(MainActivity.this, "Some other error prevented either the user from providing consent or the update from proceeding" + resultCode, Toast.LENGTH_LONG).show();

                // Some other error prevented either the user from providing consent or the update from proceeding.
            }
        }
    }

    // Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all entry points into the app.
    @Override
    protected void onResume() {
        super.onResume();

        appUpdateManager
                .getAppUpdateInfo()
                .addOnSuccessListener(
                        appUpdateInfo -> {
           // ...

                            if (appUpdateInfo.updateAvailability()
                                    == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                                Toast.makeText(MainActivity.this, "Update started where you left off", Toast.LENGTH_LONG).show();

                                // If an in-app update is already running, resume the update.
                                try {
                                    appUpdateManager.startUpdateFlowForResult(
                                            appUpdateInfo,
                                            AppUpdateType.IMMEDIATE,
                                            this,
                                            MY_REQUEST_CODE);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
    }

标签: android

解决方案


仅当当前安装的应用版本低于 Playstore 中的版本时,应用内更新才有效。在此之前,您不会看到任何弹出窗口。

如果您想检查您的代码是否正常工作,您可以使用开发人员控制台中名为“内部应用程序共享”的选项。

要使用内部应用程序共享,您需要构建一个具有更高版本和普通版本的应用程序。例如,如果您当前的版本代码是 5,则使用版本代码 6 构建一个应用程序并将其上传到内部应用程序共享启用您要检查的帐户,您将在查找时收到弹出窗口。

这些是您可以遵循的一些答案和文档。

  1. 答案 - 1
  2. 谷歌文档
  3. 谷歌答案

如需其他帮助,请发表评论。

更新:这是我在应用程序中用于触发更新的工作代码。

private AppUpdateManager appUpdateManager;
    private static final int RC_APP_UPDATE = 2;

    InstallStateUpdatedListener installStateUpdatedListener;

    installStateUpdatedListener = state -> {
                if (state.installStatus() == InstallStatus.DOWNLOADED){
                    launchSnackBar();
                }else if (state.installStatus() == InstallStatus.INSTALLED){
                    if (appUpdateManager != null){
                        appUpdateManager.unregisterListener(installStateUpdatedListener);
                    }
                }
            };
    
            StartUpdateCheck(installStateUpdatedListener);
    
    private void StartUpdateCheck(InstallStateUpdatedListener installStateUpdatedListener) {
            appUpdateManager = AppUpdateManagerFactory.create(MainActivity.this);
            appUpdateManager.registerListener(installStateUpdatedListener);
            appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
                if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
                    try {
                        appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, MainActivity.this, RC_APP_UPDATE);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                }else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED){
                    launchSnackBar();
                }
            });
        }
    
        private void launchSnackBar() {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogTheme);
            builder.setTitle("New Update Available")
                    .setMessage("A new update has been downloaded click on install to update your application and " +
                            "enjoy all new features")
                    .setPositiveButton("Install", (dialog, which) -> {
                        dialog.dismiss();
                        if (appUpdateManager != null){
                            appUpdateManager.completeUpdate();
                        }
                    });
            AlertDialog dialog = builder.create();
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }
    
    @Override
        protected void onResume() {
            super.onResume();
            if (appUpdateManager != null){
                appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
                    if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                        launchSnackBar();
                    }
                });
            }
        }

推荐阅读