0

我正在尝试将Android 应用内更新功能添加到我的裸博览会应用程序中,但是,在尝试在 MainActivities.java 中添加代码后,应用程序只是直接加载到应用程序中而不更新,即使显示版本落后.

我相信 onCreate 函数没有运行,因为应用程序只是直接加载到应用程序中,而版本落后于 Google Play 商店中的版本。但是,我不确定,因为许多其他地方可能在没有错误消息的情况下出错。

这些代码在MainActivity类内

 private AppUpdateManager appUpdateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);
////    Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
    appUpdateManager.getAppUpdateInfo().addOnSuccessListener(
                appUpdateInfo -> {
                    if ((appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE)
                            && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE))
                    {
                        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 rFragmentActivityequest.
                                this,
                                // Include a request code to later monitor this update request.
                                123);
                        }catch (IntentSender.SendIntentException e) {
                            e.printStackTrace();
                        }
                    };
                });
    }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 123) {
      if (resultCode != RESULT_OK) {
//           log("Update flow failed! Result code: " + resultCode);
      }
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    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,
                      this,
                      123);
                    }catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
              }
            });
  }

编辑:忘了提,onResume 也会导致应用程序崩溃,但即使注释掉 onResume,它似乎仍然永远无法获得更新。

4

0 回答 0