如何延迟应用程序的关闭直到所有代码执行完毕?

How to delay an app's closure till all the code finishes executing?

我有一个对话框告诉用户他们的试用已经结束,然后将他们重定向到 Play 商店的完整版本。关闭对话框后,应用程序应关闭,以便用户无法再使用它。问题是有时这会导致应用在有机会打开 Play 商店之前关闭 link。

对话框如下:

private void showTrialEndedDialog() {
    MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_regular);
    builder.setTitle("Trial ended")
            .setMessage("To continue using the app you can purchase the full version from the Play Store")
            .setPositiveButton("Go to Play Store", (dialog, which) -> {
                final String appPackageName = "com.braapproductions.redalertemulatorpro";
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                } catch (ActivityNotFoundException anfe) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                }
            })
            .setNegativeButton("Cancel", (dialog, which) -> {

            })
            .setOnDismissListener(dialog -> closeApp())
            .create().show();
  
}


public void closeApp() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        finishAndRemoveTask(); //closes the application
    } else {
        finishAffinity();
        System.exit(0); //use these two lines of code are for older versions of android
    }
}

大多数情况下,当按下按钮转到 Play 商店时,应用程序只是关闭而不是打开 Play 商店 link,尽管它有时会起作用。所以很明显,在到达 startActivity() 命令之前,应用程序正在关闭。我怎样才能让它等到一切都完成后再关闭?

您可以 post 在关闭应用程序之前使用处理程序进行延迟:

public void closeApp() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                finishAndRemoveTask(); //closes the application
            } else {
                finishAffinity();
                System.exit(0); //use these two lines of code are for older versions of android
            }
        }
    }, 3000); // 3 seconds
}

How can I make it wait till everything is finished before it closes?

不要试图“关闭”您的应用程序。

你的问题

当您打开应用商店时,activity 的实例就在您应用的任务列表中。因此,调用 finishAndRemoveTask 将关闭应用商店以及您的应用。

一个解决方案

只需使用常规 finish 调用 - 不需要终止整个任务。

.setOnDismissListener(dialog -> finish())

并且绝对避免使用 System.exit(0) - 这是一种反模式。担心您的 Activities - 让 Android 框架担心您的应用进程。

private void showTrialEndedDialog() {
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
        AlertDialog builderDialog;
        builder.setTitle("Trial ended")
                .setMessage("To continue using the app you can purchase the full version from the Play Store")
                .setPositiveButton("Go to Play Store", (dialog, which) -> {

                })
                .setNegativeButton("Cancel", (dialog, which) -> {

                })
                .setOnDismissListener(dialog -> closeApp());
                builderDialog = builder.create();
                builderDialog.show();
                builderDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
                    final String appPackageName = "com.braapproductions.redalertemulatorpro";
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                    } catch (ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                    }
                    builderDialog.dismiss();
                });

    }


    public void closeApp() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAndRemoveTask(); //closes the application
        } else {
            finishAffinity();
            System.exit(0); //use these two lines of code are for older versions of android
        }
    }