显示一次性PopUp(欢迎屏幕)

时间:2016-02-15 13:06:32

标签: java android

这次我需要在我的应用程序中使用一次性PopUp,它应检查应用程序版本并仅在第一次启动时显示对话框,如果已完成升级(如从版本2.1到2.2)。

这是我的代码:

 //Pop-Up bei erstmaligem Start
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        // second argument is the default to use if the preference can't be found
        Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

        if (!welcomeScreenShown) {
            //Popup Show

            String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
            //Changelog Text
            String whatsNewText = getResources().getString(R.string.whatsNewText);
            new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
                    R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putBoolean(welcomeScreenShownPref, true);
            editor.apply(); //Needs to be set!
        }

由于@Martyn,这段代码完美无瑕。正如我之前所说,缺少一件事:VersionCheck。

我不知道如何得到这个,我希望有人可以帮助我。

4 个答案:

答案 0 :(得分:2)

首次显示欢迎屏幕后保存versionName或versionCode。

String version = mPrefs.getString("version", "1.0");  //assuming 1.0 as first version of your app
if(!welcomeScreenShown || version.equalsIgnorCase(currentVersion))
{
    //welcome screen code
}

请参阅https://github.com/xuwupeng2000/capsitrano-scm-gitcopy#installation了解如何获取versionName或versionCode。

void*

答案 1 :(得分:1)

将您的版本存储在欢迎弹出窗口

每次应用程序启动时检查您的版本

这是获取版本的代码

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;

您可以使用此

获取版本代码
int verCode = pInfo.versionCode;

答案 2 :(得分:1)

这样做

class="row"

答案 3 :(得分:0)

PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;
String versionName = pinfo.versionName;

希望它能帮助您检查版本!

相关问题