Android:使用共享首选项更新对话框

发布于 2024-12-09 21:43:59 字数 1314 浏览 1 评论 0 原文

我正在尝试设置一个对话框,该对话框将在用户首次更新或安装应用程序后弹出。我不知道该怎么做。目前,我尝试使用包管理器来获取用户版本并进行比较以查看是否是最新的。我不确定这是否有效,因为很难测试依赖于软件包更新的内容。这是我的代码:

public void firstrun() {
    String version = "";
    String currenver = "3.9";
    // update function
    try {
        PackageInfo manager = getPackageManager().getPackageInfo(
                getPackageName(), 0);
        version = manager.versionName;
    } catch (NameNotFoundException e) {
        // Handle exception
    }
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
            .putString(version, version).commit();

    boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("firstrun", true);

    if (version.matches(currenver)) {
        // Save the state
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("firstrun", false).commit();
    } else {
        // Save the state
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("firstrun", true).commit();

    }

    if (firstrun == true) {
        new AlertDialog.Builder(this)
                .setTitle("Welcome!")
                .setIcon(R.drawable.icondialog)
                .setMessage("UpdateText")
                .setNeutralButton("OK", null).show();
    }

}

I'm trying to set up a dialog that will popup after the users updates or installs the application for the first time. I can't figure out how to exactly do this. Currently I have tried to use the package manager to get the users version and compare it to see if its the most recent. I'm uncertain if this will work as its hard to test for something that relies on a package update. Here is my code:

public void firstrun() {
    String version = "";
    String currenver = "3.9";
    // update function
    try {
        PackageInfo manager = getPackageManager().getPackageInfo(
                getPackageName(), 0);
        version = manager.versionName;
    } catch (NameNotFoundException e) {
        // Handle exception
    }
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
            .putString(version, version).commit();

    boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("firstrun", true);

    if (version.matches(currenver)) {
        // Save the state
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("firstrun", false).commit();
    } else {
        // Save the state
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("firstrun", true).commit();

    }

    if (firstrun == true) {
        new AlertDialog.Builder(this)
                .setTitle("Welcome!")
                .setIcon(R.drawable.icondialog)
                .setMessage("UpdateText")
                .setNeutralButton("OK", null).show();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-12-16 21:43:59

我认为你走在正确的轨道上,有很多解决方案 此处可能会给您一些想法。

此外,您可以创建一个包含提示或不提示标志的数据库表。默认情况下,它将设置为提示值,然后一旦用户启动并确认对话框,您就可以更改标志。然后在 SQLiteOpenHelper 的 onUpgrade() 方法中,您可以再次翻转标志,因此您的应用程序将提示安装新版本。

I think you are on the right track, there are many solutions here and here that may give you ideas.

In addition you could create a database table that contains a flag to prompt or not. By default it would be set to a value to prompt, and then once the user launches and acknowledges the dialog you could change the flag. Then in your onUpgrade() method of your SQLiteOpenHelper you could flip the flag again, and thus your application would prompt on installation of a new version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文