Reputation: 791
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
Upvotes: 0
Views: 252
Reputation: 592
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;
...
...
}
}
}
Upvotes: 3
Reputation: 719
Store the BuildConfig.VERSION_CODE
in sharedpreference. Each time user opens the app, fetch the Version code and compare with the stored data.
Upvotes: 0
Reputation: 5312
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.
Upvotes: 0
Reputation: 5222
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.
Upvotes: 1