Detect Android app upgrade and set Application class boolean for show/hide of EULA

≯℡__Kan透↙ 提交于 2019-11-28 04:22:17
EboMike

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.

Marian Klühspies

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();

Here is a simple way that can solve your problem. In your code you can easily get the app versionCode and versionName. In first install after register GCM save the versionName in sharedPreferences. In your home activity you check isAppUpdated(). if current APP VERSION_NAME is not matched with stored VERSION_NAME then re-register the GCM ID.

Here is sample code:

Save VersionName in preferences:

public static void saveVersionNameAndCode(Context context){
        try{
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            int versionCode = packageInfo.versionCode;
            String versionName=packageInfo.versionName;

            CommonTasks.showLog("Saving APP VersionCode and Version Name");

            // SAVE YOUR DATA

        }catch(Exception e){
        }
    }

Check App is Upgraded:

public static boolean isAppUpdated(Context context){
        boolean result=false;
        try{
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            int versionCode = packageInfo.versionCode;
            String versionName=packageInfo.versionName;

            String prevVersionName= GET STORED VERSION_NAME;
            if(prevVersionName!=null && !prevVersionName.equals("") &&
                    !prevVersionName.equals(versionName)){
                showLog("App Updated");
                result=true;
            }else{
                showLog("App Not updated");
            }

        }catch(Exception e){
        }
        return result;
    }

here, if isAppUpdated() return true means you have to re-register GCM ID.

Do not use app version itself, as app could be updated without EULA changing. Provide a resource having a specific version for the EULA itself. Could be a string resource "eula_version". With every change in EULA you update that string for your app's update. onCreate of your activity you check the string in the resources against the string in the SharedPreferences. The simple approach is only to check against null or not equal. (The only downside that you would display EULA again after app downgrade or deinstallation/reinstallation).

public static boolean show(Activity activity, boolean hasEULAChanged, boolean isAppUpgrade) {
    mActivity = activity;
    final SharedPreferences preferences = activity.getSharedPreferences(EULA_PREFERENCES, Activity.MODE_PRIVATE);
    final PackageInfo packageInfo = getPackageInfo();
    String eulaPref = preferences.getString(EULA_PREFERENCES, "0"); //save on accept EULA Dialog click
    String eulaActive = getString(R.string.eula_version)
    return eulaPref==null || !eulaPref.equals(eulaActive);
}

If you're targeting API > 11, then the solution from @Marian Klühspies's answer may be simplified. Registering the receiver:

<receiver
    android:name=".your.receivers.package.AppUpdateReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
    </intent-filter>
</receiver>

And the receiver itself:

class AppUpdateReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
            onAppUpdated()
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!