1

This time I need a One-Time PopUp in my Application, which should check the App-Version and shows the Dialog only on the first start and if a Upgrade was done (Like from Version 2.1 to 2.2).

Here is my Code:

 //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!
        }

This code works flawless, thanks to @Martyn. As I said before, there is one thing missing: A VersionCheck.

I have no idea how to get this and I hope someone could help me.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Daniel Kng
  • 786
  • 1
  • 9
  • 21
  • What do you mean by *version check*? – Rohit5k2 Feb 15 '16 at 13:09
  • I need to check the unnderlined value, http://i.imgur.com/MLkvYIj.png. If its newer than, a hardcoded one, eg. 0.9, it should print a PopUp. Dummycode: if(Versionbefore != VersionNow){ //Show PopUp } – Daniel Kng Feb 15 '16 at 13:18

4 Answers4

2

save versionName or versionCode after showing welcomescreen for first time.

SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.putString("version", currentVersion);

refer this for how to get versionName or 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
}
Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

store your version on welcome popup

check your version every time application starts

here is a code to get version

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

And you can get the version code by using this

int verCode = pInfo.versionCode;
AMD
  • 1,662
  • 18
  • 39
1

Do this

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
int savedVersion = mPrefs.getInt("savedVersiono", 0);
PackageInfo pinfo;
int currentVersion = 0;
try{
    pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    currentVersion = pinfo.versionCode;
}catch (NameNotFoundException ex){

}

if (!welcomeScreenShown || (currentVersion > savedVersion)) {
   //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.putInt("savedVersiono", currentVersion);
    editor.apply(); //Needs to be set!
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0
PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;
String versionName = pinfo.versionName;

Hope it will help you for checking version !

Rahul
  • 510
  • 3
  • 8