繁体   English   中英

如何在更新后首次打开应用程序时仅启动一次活动?

[英]How to launch activity only once when app is opened for first time after an update?

更新后首次打开应用程序时,启动活动最合理的方法是什么。 我知道sharedprefs是最简单的方法,但是sharedprefs在应用程序更新中是持久的,因此该选项似乎不起作用。 有任何想法吗?

使共享首选项存储该应用程序的版本号:如果版本不同,请对其进行更新,然后启动您的Activity。

编辑:这是我在我的最新检查中所做的。 它加载应用程序信息,获取版本号,如果已更改,则会弹出打开活动。

    public static boolean show(Context c)
    {
        ApplicationInfo ai = c.getApplicationInfo();
        String basis = ai.loadLabel(c.getPackageManager()).toString();
        try {
            PackageInfo pi = c.getPackageManager().getPackageInfo(c.getPackageName(), PackageManager.GET_META_
DATA);
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
            String lastVersion = prefs.getString("lastversion", null);
            SharedPreferences.Editor edit = prefs.edit();
            if(lastVersion == null){
                // save the first installed version so we can check and see when it got installed
                edit.putString("firstversion", String.valueOf(pi.versionCode));
                edit.commit();
            }
            if(lastVersion == null || pi.versionCode > Integer.parseInt(lastVersion)){
                edit.putString("lastversion", String.valueOf(pi.versionCode));
                edit.commit();
                // show it
                Intent i = new Intent(c, WhatsNew.class);
                c.startActivity(i);
                return true;
            }
        } catch (Exception e) {
            android.util.Log.v("WhatsNew", "Exception checking for release notes for [" + basis +
"]:" + e.getMessage(), e);
        }
        return false;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM