【发布时间】: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