Android: First run popup dialog

前端 未结 6 2180
感情败类
感情败类 2020-12-04 11:50

I am trying to make a popup dialog that only shows after the app\'s first run that will alert users of the new changes in the app. So I have a dialog popup like this:

<
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 12:15

    Like this you can make a difference between a new install and a update.

    String StoredVersionname = "";
    String VersionName;
    AlertDialog LoginDialog;
    AlertDialog UpdateDialog;
    AlertDialog FirstRunDialog;
    SharedPreferences prefs;
    
    public static String getVersionName(Context context, Class cls) {
        try {
            ComponentName comp = new ComponentName(context, cls);
            PackageInfo pinfo = context.getPackageManager().getPackageInfo(
                    comp.getPackageName(), 0);
            return "Version: " + pinfo.versionName;
        } catch (android.content.pm.PackageManager.NameNotFoundException e) {
            return null;
        }
    }
    
    public void CheckFirstRun() {
        VersionName = MyActivity.getVersionName(this, MyActivity.class);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        StoredVersionname = (prefs.getString("versionname", null));
        if (StoredVersionname == null || StoredVersionname.length() == 0){
            FirstRunDialog = new FirstRunDialog(this);
            FirstRunDialog.show();
        }else if (StoredVersionname != VersionName) {
            UpdateDialog = new UpdateDialog(this);
            UpdateDialog.show();
        }
        prefs.edit().putString("versionname", VersionName).commit();
    }
    

提交回复
热议问题