Detect if new install or updated version (Android app)

前端 未结 8 1089
-上瘾入骨i
-上瘾入骨i 2020-12-24 06:41

I have an app on the Play Store. I want to put a requirement that if users want to use a certain part of the app, they have to invite a friend before being able to do so. Bu

8条回答
  •  死守一世寂寞
    2020-12-24 06:48

    If you want to perform any operation only once per update then follow below code snippet

    private void performOperationIfInstallFromUpdate(){ 
    try {
            SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
            String versionName = prefs.getString(versionName, "1.0");
            String currVersionName = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            if(!versionName.equals(currVersionName)){
                //Perform Operation which want execute only once per update
                //Modify pref 
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString(versionName, currVersionName);
                editor.commit();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return BASE_VERSION;
        }
    

    }

提交回复
热议问题