Detect Android app upgrade and set Application class boolean for show/hide of EULA
It's much easier to just check your current app version.
PackageInfo packageInfo = activity.getPackageManager()
.getPackageInfo(activity.getPackageName(), 0);
int versionCode = packageInfo.versionCode;
When your app starts, you check your SharedPreferences for an integer value with the version code. If there is none, or if it doesn't match, display the EULA. After the user accepts the EULA, write the versionCode value to the SharedPreferences.
versionCode
will match the version number you store in the Manifest.
According to the original approach, I´ve managed it to detect if the app is upgraded by using BroadcastReceiver
public class YourUpgradeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Uri packageName = intent.getData();
if(packageName.toString().equals("package:" + context.getPackageName())){
//Application was upgraded
}
}
}
And in your Manifest
<receiver android:name=".YourUpgradeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
Detect If App Has Been Updated
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
int versionCode = sharedPreferences.getInt("VERSION_CODE", BuildConfig.VERSION_CODE);
if(versionCode != BuildConfig.VERSION_CODE) {
onAppUpdated();
}
sharedPreferences.edit().putInt("VERSION_CODE", BuildConfig.VERSION_CODE).apply();