首页 > 解决方案 > 无法使用 Play 核心库提供的支持应用内更新来更新应用

问题描述

我只是想在谷歌播放核心库 API 提供的应用程序更新服务中实现。

将较新版本的应用程序发布到内部测试轨道后,我在 Play 商店中获得了应用程序的更新版本

在此处输入图像描述

但是,我无法在同一页面上看到“更新”按钮

在此处输入图像描述

此外,App 无法根据代码的实现进行更新。请帮助我解决这个问题。

private AppUpdateManager appUpdateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        appUpdateManager = AppUpdateManagerFactory.create(this);
        InAppUpdate.setImmediateUpdate(appUpdateManager, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        InAppUpdate.setImmediateUpdateOnResume(appUpdateManager, this);
    }
}

public class InAppUpdate {

    public static int REQUEST_APP_UPDATE = 302;

    public static void setImmediateUpdate(AppUpdateManager appUpdateManager, Activity activity) {

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

                            // Checks that the platform will allow the specified type of update.
                            if ((appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE)
                                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                                // Request the update.
                                try {
                                    appUpdateManager.startUpdateFlowForResult(
                                            appUpdateInfo,
                                            AppUpdateType.IMMEDIATE,
                                            activity,
                                            REQUEST_APP_UPDATE);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
    }

    public static void setImmediateUpdateOnResume(AppUpdateManager appUpdateManager, Activity activity) {

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

                            if (appUpdateInfo.updateAvailability()
                                    == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                                // If an in-app update is already running, resume the update.
                                try {
                                    appUpdateManager.startUpdateFlowForResult(
                                            appUpdateInfo,
                                            AppUpdateType.IMMEDIATE,
                                            activity,
                                            REQUEST_APP_UPDATE);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
    }

    public static void setFlexibleUpdate(AppUpdateManager appUpdateManager, Activity activity) {

        InstallStateUpdatedListener installStateUpdatedListener = installState -> {
            if (installState.installStatus() == InstallStatus.DOWNLOADED) {
                Snackbar snackbar =
                        Snackbar.make(
                                activity.findViewById(android.R.id.content),
                                activity.getString(R.string.in_app_snack_bar_message),
                                Snackbar.LENGTH_INDEFINITE);
                //lambda operation used for below action
                snackbar.setAction(activity.getString(R.string.in_app_snack_bar_action_title), view ->
                        appUpdateManager.completeUpdate());
                snackbar.setActionTextColor(activity.getResources().getColor(R.color.in_app_snack_bar_text_color))
                ;
                snackbar.show();
            } else
                Log.e("UPDATE", "Not downloaded yet");
        };

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

                            // Checks that the platform will allow the specified type of update.
                            if ((appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE)
                                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                                // Request the update.
                                try {
                                    appUpdateManager.startUpdateFlowForResult(
                                            appUpdateInfo,
                                            AppUpdateType.FLEXIBLE,
                                            activity,
                                            REQUEST_APP_UPDATE);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

        appUpdateManager.registerListener(installStateUpdatedListener);
    }

    public static void setFlexibleUpdateOnResume(AppUpdateManager appUpdateManager, Activity activity) {

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

                            if (appUpdateInfo.updateAvailability()
                                    == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                                // If an in-app update is already running, resume the update.
                                try {
                                    appUpdateManager.startUpdateFlowForResult(
                                            appUpdateInfo,
                                            AppUpdateType.FLEXIBLE,
                                            activity,
                                            REQUEST_APP_UPDATE);
                                } catch (IntentSender.SendIntentException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
    }
}

标签: javaandroidgoogle-playin-app-updategoogle-play-core

解决方案


问题可能是您正在使用通过 USB 电缆而不是从 PlayStore 安装的应用程序。从 Playstore 安装您的应用程序,然后它就可以工作了。另请注意,当使用应用内更新时,您的应用的缓存可能会阻止它识别新上传的更新,因此请重新启动您的应用几次,如果需要删除缓存以强制它从谷歌播放重新加载数据


推荐阅读