Я попробовал пример кода из Hello Gui, поставляемого с install4j8.0.2, чтобы загрузить обновление моего приложения, используя Фоновый загрузчик обновлений . Когда загрузка будет завершена, я хочу показать пользователю диалог подтверждения, хотят ли они запустить программу обновления сейчас или позже. Проблема в том, что средство обновления не запускается сразу, а запланировано на следующий раз, когда запускается программа запуска.
В общем, мое требование:
- Обновление загрузки в фоновом режиме
- Показать диалоговое окно подтверждения после завершения загрузки
- Запустить программу обновления немедленно, если пользователь хочет обновить ее сейчас.
Я не могу выполнить sh, так как Обновление планируется запустить при следующем запуске средства запуска.
Я вижу, что UpdateChecker.isUpdateScheduled()
возвращает false по окончании загрузки.
Вот пример кода для немедленного запуска средства обновления:
private void downloadAndUpdate() {
// Here the background update downloader is launched in the background
// See checkForUpdate(), where the interactive updater is launched for comments on launching an update downloader.
new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
// Note the third argument which makes the call to the background updater blocking.
ApplicationLauncher.launchApplication("1160", null, true, null);
// At this point the update downloader has returned and we can check if the "Schedule update installation"
// action has registered an update installer for execution
// We now switch to the EDT in done() for terminating the application
return null;
}
@Override
protected void done() {
try {
get(); // rethrow exceptions that occurred in doInBackground() wrapped in an ExecutionException
if (UpdateChecker.isUpdateScheduled()) {
JOptionPane.showMessageDialog(null, "Download is complete, the new version will now be installed.", "Hello",
JOptionPane.INFORMATION_MESSAGE);
// We execute the update immediately, but you could ask the user whether the update should be
// installed now. The scheduling of update installers is persistent, so this will also work
// after a restart of the launcher.
executeUpdate();
} else {
JOptionPane.showMessageDialog(null, "Update could not be downloaded", "Hello", JOptionPane.ERROR_MESSAGE);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "An error has occurred:" + e.getCause().getMessage(), "Hello", JOptionPane.ERROR_MESSAGE);
}
}
}.execute();
}
private void executeUpdate() {
// The arguments that are passed to the installer switch the default GUI mode to an unattended
// mode with a progress bar. "-q" activates unattended mode, and "-splash Updating hello world ..."
// shows a progress bar with the specified title.
UpdateChecker.executeScheduledUpdate(Arrays.asList("-q", "-splash", "Updating ..."), true, null);
}