I will be releasing an update for my app with a new data structure, therefore if a user is updating my app I need to update their current data. So I was wondering how can I programatically check if the user updated my app or installed a new copy (if a new copy is installed I don't need to update anything) ?
SQLite
If your data is in an SQLite database, you can implement code in SQLiteOpenHelper.onUpgrade().
Checking previous version
If not, you need some kind of way to check the previous version that was open on the device. Something like:
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
// Use SharedPreferences to store something like "LAST_VERSION_RUN" = pInfo.versionName
Then, you can check if previous version is not equal to current version. Of course, this kind of code has to be in previous versions, also. There is no way to implement, once an app is already in the wild, a code that will detect whether an app has been upgraded.
Checking current version
I suppose what you could do in this case is detect the app version, and then set a flag to not update the data schema again. So, let's say your app version was 1.0.2, and is now 1.0.3:
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
if (pInfo.versionName.equals("1.0.3") && /* some SharedPreference value */ == false) {
// upgrade
// set SharedPreference value to true
}
I think that's going to be the best way to implement this if you're not using SQLite.
Related
I have an app with a Login functionality and also auto Login, and before login screen, a Splash Screen Activity is displayed. Inside Splash Screen activity I want to check if the user has just updated the app from the Google play to a newer version, and if so, to always show him the Login screen.
So my question is, how to check if current version app is different from the last version?
I tried solutions found in this topic: Detect if new install or updated version (Android app)
But in this function
public static boolean isInstallFromUpdate(Context context) {
try {
long firstInstallTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
long lastUpdateTime = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).lastUpdateTime;
return firstInstallTime != lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
This line is always true: firstInstallTime != lastUpdateTime;, but after an update, if I close the app and then reopen it, this line should be false.
If you have any suggestions or need more details, please let me know! Thanks
I think a simple solution is if you have an admin panel of mobile app for data calling then you have to store version detail latest and these data related API call into your app so easily detect if you have to change the version in admin panel then show a dialog of update app and redirect to play store...
This is my personal opinion because I have used this kind of method .....
You can simply set a SharedPreference, if you open the app after an update. After you closed the app and reopened it, you should check for your isInstallFromUpdate(...) and the just set SharedPreference
I know we can define a custom permission in one app and other app can ask for this permission using uses_permission tag.
My problem is, I have 2 apps and one app is dependent on other to work as expected. As both the apps will be updated from play store over time, their versions has to be in sync so that they can interact and work without any issues.
User should be able to install/update app B to version 2 only if they have already installed/updated version 2 of app A.
My idea is, if this can be handled by defining something (uses_feature where we can define version also, just like glesVersion) in manifest file itself, then user will not see update available of app B, if they have not updated app A already.
I hope I have explained my problem clearly. Please suggest some solution.
While opening your launcher activity all you ought to do is check
the version of that package you have installed and then show them the
update.
You need to figure out what is the right package name of your installed application on your device.
Getting Package Info:
PackageInfo pinfo = null;
pinfo = getPackageManager().getPackageInfo("com.yourapplication.android", 0);
//getVersionCode is Deprecated, instead use getLongVersionCode().
long verCode = pinfo.getLongVersionCode();
//getVersionName is Deprecated, instead use versionName
String verName = pinfo.versionName;
Then you can check in here like,
if(verName.contentEquals("your new version") {
// do something such as update
}
else {
// function normally
}
Hope it helps!
I have a SharedPreference that counts the amount of launches of my App. I expect this to be 0, when I install my app. Nevertheless it is 14. The same strange behaviour I observe with my database, that already exists on a fresh install.
I didn't intent to recycle my app data (like in the Google Cloud). On my device in the account settings the app-data sync is on. If I turn it off, and make a reinstall I observe the same behaviour.
Anyone has every observed such a behaviour? Is there any way to prevent recycling old data and force a proper install?
In Android Marshmallow Google introduced the "Auto Backup" feature which is turned on by default if your targetSdkVersion is >=23.
This will back up your database and SharedPreferences by default and restore it when you re-install the application. To turn this feature off you have to add android:allowBackup="false" to your AndroidManifest.xml.
More info here: Auto Backup for Apps
Review your code in AndroidManifest on tag application if it has android:allowBackup="false". If you don't have (by default it's true), your app participates on android backup and restore infrastructure and can happen exactly what you say.
More information in this post: What is "android:allowBackup"?
This needs to be handled from app side regarding shared Prefs.
Created a shared preference helper class and in the helper class have the below condition.
private static String SHARED_PREFS_VERSION = "SharedPrefsVersion"; // In this save the version of current shared Prefs.
void SharedPrefsHelper() {
if( BuildConfig.Version > getSharedPrefsVersion() ) {
SharedPreferences.Editor editor = prefs.edit(); editor.clear(); // Clear all the shared Prefs and update the current version.
SetSharedPrefsVersion(BuildConfig.Version);
}
}
For further reference look at :
https://stackoverflow.com/a/12381061/7364024
I am trying to figure out how to forcefully update the latest version from play store.
As the older version of my android app is already in play store where i haven't implement the check to get the current version of the app and compare with the play store app version like specified in the link update android app forcefully
I want that when I upload my new version on play store then user should not be able to use the app till he update the new version.
Could you please help me out.
I have to do the same thing but what i did i gonna share you i hope this will help you.
1> I get the previous code version of my application and store it the string.xml. like my live app version code is 1. And my new app version code is 1.1.
This code is tell us our app version code.
PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;
String versionName = pinfo.versionName;
2> Than check
if (newcodeversion > previouscodeversion) {
// open any activity or pop up
}
3> And in the new activity place Text view with text "you have update version" and a button.And on the click of that button navigate to google play store.
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("Your application URL"));
startActivity(i);
It is not possible if the logic of comparing the versionCode and blocking the user isn't already placed inside the current app on PlayStore.
This is not difficult to do. Simply bar the user from having the core functionality of what ever your app is responsible for.
Example, if it is a calling app, do a version check, and if the user has not updated as yet, restrict the use of call making entirely.
According to #pieter-b, taken from Forcing Updates:
For this to work, some kind of logic the app uses needs to be under your control. And then without that part of logic the app won't work
and you can show a message: "your app is out of date, please download
the new version to continue use."
Consider a messenger app where traffic goes through your servers. Just
refuse to deliver messages of clients using an outdated version.
Also refer to Force update android app when new version available.
IThe scenario is that my application search all installed application in order to determine versions of those applications. Then store name of the application with its versions on the same file. If new version of the application is available, then give notification to the user. The question is "how can I learn the version of the other application from my application ?"
Thanks for all your help
Something like this should work to get the current version:
List<PackageInfo> packages = getPackageManager().getInstalledPackages();
for(int i=0;i<packages.size();i++)
{
int version = packages.get(i).versionCode;
// do whatever with it here
}
Getting the most recent version in the Play Store doesn't really work, though. Like A--C said, you could check versionName instead, and just notify as updated if the two aren't exactly the same(don't worry about higher/lower, just inequality).
You can do following (inside of Activity or any other Context)
getPackageManager().getPackageInfo(getPackageName(), 0).versionName
getPackageManager().getPackageInfo(getPackageName(), 0).versionCode