【问题标题】:How to delay an app's closure till all the code finishes executing?如何延迟应用程序的关闭直到所有代码完成执行?
【发布时间】:2021-06-04 20:36:58
【问题描述】:

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

这是对话框:

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 商店链接,尽管有时它确实有效。所以显然应用程序在到达startActivity() 命令之前就被关闭了。我怎样才能让它等到一切都完成后再关闭?

【问题讨论】:

    标签: java android android-dialog


    【解决方案1】:

    我怎样才能让它等到一切都完成后再关闭?

    不要试图“关闭”你的应用。

    您的问题

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

    解决方案

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

    .setOnDismissListener(dialog -> finish())
    

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

    【讨论】:

    • 让我试试然后报告
    • "finishAndRemoveTask 将与您的应用一起关闭应用商店"。不完全正确。在 prelollipop 上会发生这种情况。但在 lollipop+ 上,它不会关闭“play store”,因为应用发送给 play store 应用的意图会为新活动(play store)创建一个新任务。
    • "app发送到play store app的intent为新activity(play store)创建了一个新任务"——app发送的intent由app控制。除非它指定一个“新任务”标志,否则它不会神奇地产生一个新任务。我在运行最新 Android 的 Pixel 3a 上进行了测试,但它按预期失败了。
    • 在您的像素 ​​3a 上尝试我的答案中的代码,您会看到。
    • 正如您在回答的 cmets 中所讨论的,很明显,代码无法执行 OP 尝试执行的操作。
    【解决方案2】:

    您可以在关闭应用程序之前使用处理程序发布延迟:

    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
    }
    

    【讨论】:

    • 有更优雅的方式吗?
    • @purchaseTest 也许你可以在onStop() 回调中调用closeApp(),这意味着只要Play商店打开,应用就会进入后台......但只要确保你刚刚关闭了对话框跨度>
    • 你的意思是onPause()?
    • 两者都可以,onPause()onStop 之前被调用,但请确保在其他时间不调用它,而不是刚刚打开对话框
    【解决方案3】:
    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
            }
        }
    

    【讨论】:

    • 但是对话框可以通过按下后退按钮或按下它的外部来关闭
    • @purchaseTest 现在检查。我稍微编辑了代码。现在,当您单击肯定按钮时,它不会关闭,直到 onClick 方法中的所有代码完成。
    • 所以我试了一下,结果和原来一样,这意味着应用程序经常在 Play 商店打开之前关闭
    • 可能问题出在您的 Internet 连接上。如果执行了正按钮内的代码并且没有互联网连接,则不会打开 Playstore;除非您没有安装 Play 商店应用,在这种情况下,它会打开您的浏览器应用来搜索您的目标应用。
    • 问题不是网络连接。该建议与原始问题基本相同-您只是将逻辑从肯定按钮单击移动到...单击肯定按钮...无论哪种方式,您都在做我在回答中解释的事情:取消任务里面有 Play 商店应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2013-08-16
    相关资源
    最近更新 更多