Android PlayStore 'InAppUpdate' 绝对强制升级(必须立即升级,不能取消任何选项)

Android PlayStore 'InAppUpdate' make absolutely force the Upgrade (Must Immediately Upgrade with no option to cancel what so ever)

我有下面的代码,我已经成功地实现了 InAPPUpdate 代码,根据下面的屏幕截图,它工作正常。但是,在此实现中,用户仍然可以使用 (X) 关闭按钮取消 InAppUpdate,并且在下载应用程序更新时用户仍然可以取消更新。

我的问题是有没有办法绝对强制最终用户更新应用程序?除非他们更新,否则我需要阻止他们使用该应用程序。

N.B: (请不要问为什么要强制不是讨论的问题)

public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener, MainView {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //check for updates
        validateAppUpdate();
}

//in app updates
    private void validateAppUpdate() {
        // Creates instance of the manager.
        appUpdateManager = AppUpdateManagerFactory.create(getBaseContext());

        // 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(new OnSuccessListener<AppUpdateInfo>() {
            @Override
            public void onSuccess(AppUpdateInfo appUpdateInfo) {

                Log.e("AVAILABLE_VERSION_CODE", appUpdateInfo.availableVersionCode()+"");
                Log.e("AVAILABLE_VERSION_CODE_2", appUpdateInfo.updateAvailability()+"");
                //Log.e("html", "Error loading html/index.html", e);
                 if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                        // For a flexible update, use AppUpdateType.FLEXIBLE
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    // Request the update.

                    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.
                                MainActivity.this,
                                // Include a request code to later monitor this update request.
                                UPDATE_REQUEST_CODE);
                    } catch (IntentSender.SendIntentException ignored) {

                    }
                }else{
                    System.out.println("No Update Available.");
                }
            }
        });

        appUpdateManager.registerListener(installStateUpdatedListener);

    }
    //lambda operation used for below listener
    InstallStateUpdatedListener installStateUpdatedListener = installState -> {
        if (installState.installStatus() == InstallStatus.DOWNLOADED) {
            popupSnackbarForCompleteUpdate();
        }
    };


    private void popupSnackbarForCompleteUpdate() {

        Snackbar snackbar =
                Snackbar.make(
                        findViewById(android.R.id.content),
                        "Update almost finished!",
                        Snackbar.LENGTH_INDEFINITE);
        //lambda operation used for below action
        snackbar.setAction(this.getString(R.string.APP_NAME), view ->
                appUpdateManager.completeUpdate());
        snackbar.setActionTextColor(getResources().getColor(R.color.browner));
        snackbar.show();
    }

您需要在扩展 Activity 的 class 中重写 onActivityResult(),如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK) {
        validateAppUpdate();
    }
}

这将 运行 validateAppUpdate() 直到应用更新。