How can I run some code when the user updates my app? - java

I would like to upgrade my app to android market and some users have the old version of the app. What I want to do is in my new app, I want to run some codes to change a bit on the app database when the user do the update of my app, so how can I check if the user has the older version of my app in their mobile?

You should use a SQLiteOpenHelper-class to manage your Database (tutorial).
In your new App's version, you simple increase the version-number of the Database so the onUpgrade()-method gets called and you do your work on the Database.

I'd do the transformation the first time your code is run after the update: there's no need to do it any earlier.
And then I'd take the usual approach to this: once you've done the update, write some field to the database that indicates the current version. And when that field equals the current version then you know that no update needs to be done.

Well, if it's only the database you want to change take a look at SQLiteDatabase.onUpgrade. For more information look at IE http://www.barebonescoder.com/2010/05/android-development-database-management/
If it's not just the database I would use Preferences to keep a reference on which version the user has and if the user has run the latest update routine.

Related

Getting play-store version of the application

I tried using JSOUP library to get the application version for the play-store website.
But it's not working as google has updated their website structure recently.
I am working on java platform.
What's the best way to get the version of the application.
if your purpose is to provide update control you can use AppUpdateManager. Thanks to AppUpdateManager, you can check if there is an update, show an automatic message to the user and download the update. At the same time, it offers 2 options called IMMEDIATE and FLEXIBLE. With these options, you have the chance to make the update mandatory or optional.
for more : https://developer.android.com/reference/com/google/android/play/core/appupdate/AppUpdateManager

Update database on server when app is being uninstalled

I know there might be many questions like my question. But It is different. Actually, I am making a static chart that will show how many devices currently having my Android app installed and how many devices have uninstalled it.
For this, I am creating a uniqueID when the app is installed on a device and saving uniqueID along with FCM token to SQL database on the server.
To Create uniqueID:
uniqueID = UUID.randomUUID().toString()
Now, while I am saving every device with a token and uniqueID to the database. Of course, the device will be considered to have active app installation even when uninstalled the app.
So, I want to add a field in the database as inactive against the device that has uninstalled the app. To achieve this, I am thinking to send a request to the database and update the information when the app uninstallation is triggered.
Is this possible? And if yes, then can anyone please tell me how. Or are there any other method to achieve this. Thanks in advance.
Yes it it possible. You can send a push notification to your app to all your active users everyday from your backend side, and on your Android side, call an API on your server to confirm that you exist. If a client does not confirm his existence in a period of time (like 3 days), you know they have uninstalled the app. This is what Adjust and other statistics do for uninstall statistics.
You can't easily run your own code when an app is uninstalled. It is possible to run code in another app, but that requires that you get the user to install both apps.
In addition to the approach Adib described, you can consider using Google Analytics for Firebase to detect uninstalls of your app by Android users. As shown in the answer to this question, Firebase automatically tracks uninstalls in that case.
Maybe you want to use Firebase Analytics and the event app_remove. Mark it as a conversion and use Functions to remove the user from your Firebase Project(if anonymous) and/or any other data associated with the user(Storage, Database, Firestore, etc.) or anything else you might want to do upon user removal of the app.

updating new version on play store that would cause previously installed apps to crash on update

I have an application(1.0) on store. I have used Couchbase in it to store data for offline use. Now, for v1.1 I have switched to SQLite and changed Data structure in some places, so on updating to 1.1 the app may crash or wont show any data because there will be no data in new database(SQLite).
To prevent above issue I have created a migration/setup-data method(only run one time after v1.1 update) which is downloading and dumping data in SQLite.
I want to know if there is any other technique or way to achieve backward compatibility ?
Typically this is done by subclassing SQLiteOpenHelper to manage your database. It sounds like you're creating the DB for the first time, so you could just import the data in the onCreate() method after you set up your schema.
SQLiteOpenHelper takes care of tracking the version of the database and only running the onCreate/onUpgrade methods when needed.

Overwrite app and deletes database on Android

I am creating multiple apps and every app has same package so if I install one then it should overwrite the other, i.e. one of these apps can only be installed in the phone. This may sound weird but this is the way apps need to be. I have all the apps ready but I have this issue that I want to drop the table of database on every install, so if I install second app it overwrites the first one but the database is not deleted. I cannot change the VersionCode as I won't be able to overwrite with the older version so all apps have same package and same version code.
Basically it should ask for a password on every first run of the app, then I create a table and store it there that this user is authenticated, so for every new app this has to be done again but on update the table remains there which results in no prompt for password. Now how do i detect and drop the table from database. Even if I use sharedpreferences instead of database, they remain there on the update and I can't even detect if the app is updated as version code is same.
I will really appreciate if anyone can give me a solution or a hint.
Thank you,
Hamza Manzoor
You could store a seperate version value of the apps in the SharedPreferences and check the value in it against a list or numerical value and decide depending on the result.
You could also use Android's built-in version (not the nummrical value but the string value).
If you go for the SharedPreferences solution, you can use a kind of algorithm for the version value. Let's say one app is numbered "A", another "B" and yet another "C". You then append the app's real version to that, i.e. "A1.0" or "B2.4", ... (you get the idea). This way Android has no idea of what app is curretly being installed.
Whenever you start an app just check for the first value. If that is different from the app's normal value you know, it has been replaced. Otherwise check for the version value and see, whether you need to update the database or not.
// May I add:
Of course this is a possible source for bugs but if you are careful this should work. If someone has a better solution, please post.
Your app will not be overwritten\upgraded via Google Play mechanism in case they will have the same versionCode. Only via "adb install -rd" or some root-enabled side-loader.
IMHO, the right way to act is
increase versionCode and DB SCHEMA_VERSION on each upgrade as usual
in your DB class (which extends SQLiteOpenHelper) in onUpgrade() method you should check DB version and set some variable in Prefs accordingly, later in Activity you'll be able to fire a password dialog (open DB on onCreate(), check prefs on onResume()).

How to force update on an app when backend-changes requires an updated app

I have read different posts about forcing an update of an app, and the only idea that ppl has had (as far as I can tell) is to create a web service which the app calls to see what version is the current one. The WS must them manually be updated to reflect the versionCode.
This approach has several problems:
Problem 1:
When you upload an app to the Play Store, then it takes several hours before it is available for devices to download, and you never know when. Also, it seems to me like it becomes available at different times for different devices, so you really have no clue what so ever when its "available for all".
This means that you cannot immediately update the WS (that tells the app what is the "current" version) since directing them to Play Store won't be very constructive, as the updated app isn't available there anyways. And since you don't know when it will be available, you don't really know when to update the WS.
Problem 2:
Sometimes you do some changes to the back-end (comm protocol changes or something else) that requires you to upload the new version of the app and then update the backend so that they can talk to each other as expected. In this case, you really want some way to tell the app that "please update since you are out of date" and direct them to the Play Store.
So, the question is, how can I achieve this functionality?
As zapl has commented, the backend should ALWAYS allow backward compatibility. Whenever your app makes a call to the backend server the app should report its version number and the backend should respond accordingly.
As for forcing an app update, you should look at GCM to push a notification which will then take the user to the update although in reality this wouldn't actually 'force' the user to update.
Either way, as long as both the backend and app report their version numbers to each other (perhaps with a notification of 'update available') then you can design things so 1). they continue to work and 2). users will update ASAP when they need to.
I understand that publishing to Google Play can have delays but as long as the backend retains backward compatibility, it shouldn't be a problem.
You can use appgrades.io to force app updates using custom view/popup that you can design (no code required) on appgrades Dashboard

Categories

Resources