简体   繁体   中英

How to find out that application was updated

Is there a way to do something in code only when application was updated directly from Google Play (or automatically)?

My task is to ask user about some terms of privacy police just after installing or after every updating

You can use android.intent.action.PACKAGE_REPLACED receiver to detect when your app is updated. According to doc:

Broadcast Action: A new version of an application package has been installed, replacing an existing version that was previously installed. The data contains the name of the package.

AndroidMenifest.xml: Register a receiver with android.intent.action.PACKAGE_REPLACED action.

<receiver android:name=".OnUpgradeReceiver">
  <intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" android:path="your.app.package" />
  </intent-filter>
</receive>

AppUpdateReceiver.java: Create broadcast receiver that will trigger when your app is updated. Check for your version code and perform the specific action.

public class AppUpdateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {

        switch (BuildConfig.VERSION_CODE){
            case 1:
                //DO something if the version code is 1.
                break;
            case 2:
                //DO something if the version code is 2.
                break;
            ...
            ...
        }
    }
}

Simply check if BuildConfig.VERSION_CODE has changed. To do this save the version code in the SharedPreferences . Application#onCreate() could be a good point for this.

You can update the code simply (of showing policy to the user on launch) and when the user would launch the app next time it should show up. Maybe keep a preference value to ensure it only happens once.

Alternatively, you can also read the current version code on each launch and keep storing it as a preference and once it gets updated, you can show the policy and also update the stored value.

Store the BuildConfig.VERSION_CODE in sharedpreference. Each time user opens the app, fetch the Version code and compare with the stored data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM