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

Java 更新后首次打开应用程序时,如何仅启动一次活动?,java,android,sharedpreferences,Java,Android,Sharedpreferences,更新后第一次打开应用程序时,启动活动的最合理方式是什么。我知道SharedPref是最简单的方法,但是SharedPref在应用程序更新中是持久的,所以这个选项似乎不起作用。有什么想法吗?让共享pref存储应用程序的版本号:如果版本不同,请更新它,然后启动活动 public static boolean show(Context c) { ApplicationInfo ai = c.getApplicationInfo(); String bas

更新后第一次打开应用程序时,启动活动的最合理方式是什么。我知道SharedPref是最简单的方法,但是SharedPref在应用程序更新中是持久的,所以这个选项似乎不起作用。有什么想法吗?

让共享pref存储应用程序的版本号:如果版本不同,请更新它,然后启动活动

    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;
    }
编辑:这是我在“最新情况”检查中所做的。它加载应用程序信息,获取版本号,如果已更改,则弹出打开活动

    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;
    }

将应用程序的版本号设置为shared pref store:如果版本不同,请更新它,然后启动活动

    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;
    }
编辑:这是我在“最新情况”检查中所做的。它加载应用程序信息,获取版本号,如果已更改,则弹出打开活动

    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;
    }

你能更清楚地回答你的问题吗?到目前为止,您似乎只想允许应用程序的一个实例,这可以通过AndroidManifest.xml完成。不过,我相当肯定,我没有完全理解这个问题。你能更清楚地回答你的问题吗?到目前为止,您似乎只想允许应用程序的一个实例,这可以通过AndroidManifest.xml完成。但是我很确定我没有完全理解这个问题。太好了,我感谢你的时间。太好了,我感谢你的时间。