Code to be executed on Android app upgrade - android

I want to execute some code when android app is upgraded.
I read one way is to store the version info in shared preference/db and then compare on application onCreate and execute your logic.
Does onCreate in Application run on upgrading?
Android: How to reset FirstRun SharedPreferences when my app is updated?
But since the post dates back to 2013. Was just wondering if any new better ways have emerged out .
cheers,
Saurav

Related

How to run a particular code only during the installation of the app?

I am working on SQLite and i want that a particular code should only be executed while the app is being installed . I have read that this can be achieved using Shared preference but i'm unable to understand how it works. Can somebody please explain..
I want to copy messages from sms/inbox to my app's database which should happen only once during installation and only the new arrived messages should be added to the database after that.
Use Shared Preferences to read and store a key "firstrundone" or "codeexecuted". If false then run the code and set on true when done.

Upgrade Android app and keep old SQLite database

I'm writting a small app for gym exercises. I keep my records in SQLiteDatabase and I want this app to start running as fast as I finish main part of the app. But further I want to develop this app and add some more features. There will be some records in a database yet. How can I save this records and move it to new version of the app? I'm developing with Android Studio, and each run app is installed from the scratch(before previous version is unistalled and all data is deleted?) if there were any changes, right? So, how can I keep old records and add them to new version app? I have seen some answers with onUpgrade, but it wasn't that specific that i can understand them, beacause they don't tell anything about upgrading only app, not the databse. And sorry for my English :)
Installing a new version of your app doesn't necessarily remove its data (unless you uninstall it manually before installation or just clear data manually).
Also, if you change your database structure in an update, you can override
SQLiteOpenHelper's onUpgrade method in order to handle database updates properly.

How to run code only when my app is updated [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to know my Android application has been upgraded in order to reset an alarm?
I need to run a certain code when my android app is only updated (I specifically want to reschedule alarms).
The did an extensive search on SO and I found that people suggest using SharedPreferences. I dont think this would be correct as sharedPreferences can be deleted upon restart or by simply clearing the cache. Which means when my app is updated, shared preferences may not exist and I may not know if it is an update or simply fresh install ( or even worse a restart).
So, Is there any other way? Perhaps some sort of intent or some sort of method that executes (kinda like onUpgrade for sqlite).
Please advise . Thank you so much
SharedPreferences is the best approach. It is cleared if a user un-installs and installs again, in addition to what you described. If this is not good enough for you, you will need to save the last installed version on a server, but that means you can not run your test when offline.
onUpgrade for sqlite simply check if database is present, then what is its version. If Version of current DB is lower then the one in APK it will clear the DB and updated the DB with new one.
Similarly if you clear the application cache. All the file including the DB will be cleared
Similarly you can implement in you application:
like have a static public int version = 1000; in your app. On application on create check if SharedPreferences contains version ( even if SP is not present ) it will return saved or default method.
If value of SP version if less then the current version in your code. Use the updated function which you intent to use.
And if user uninstalls and reinstalled the app then too you might want to register your calls again.

How to delete the data while re-install the app

Thanks for previous replies,
is it possible to delete the stored content from sqlite once re-install the application. I am storing data in database, once i re-install the same app again, the previous data still stores in sqlite. i want to delete the stored content while re install the app. i am not sure about this.
This seems like a hack and maybe not the actual answer, but can you -- with each new version of your app -- increment an identifier (from 10 to 11) in the code and then check against a stored preferences containing that identifier. If you have a constant in your code that is higher than the identifier stored on the previous device, then you can clear the database to whatever you think its state should be. Then with each new released version of the app you increment this number..
Edit: In API level 9 and higher, you can -- whenever your app starts up -- write the date in which the app was installed (see here for an explanation on how to find the install date). If you check that it was installed after the date which is written, kill the data!
Thanks for all replies,
I maintain the Version code for all the builds, once i re-install the application, i check the version, if i found the version changes, i simply remove the DB. this case only applicable for overriding the install of APk(ie without un-installing the application). When we made a uninstall, the internal data and sqlite for the application ill automatically deleted.
sqlite databases are stored as files on your file system, to delete the data. You just need to delete the file.
What you'd want to do is setup some way of detecting if the app is being run for the first time, if this is the first time the app has been run then check the database exists, if it does delete it. Then recreate the database as empty.
Or you could go through and remove all the data in each table/drop each table in the database on the first run if the database exists.
Read here : Detect Android app upgrade and set Application class boolean for show/hide of EULA
Create a UpgradeBroadcastReceiver of your own that will run the delete instructions you want and register it in your manifest file.
When you delete the app, then the database should be deleted too. Unless you go to some trouble to keep it. If you simply update the app, then the data should be kept. If you need to delete the data upon reinstall, try this:
Every time you start an Activity, call PackageManager.getPackageInfo() and check lastUpdateTime. Compare it with a time stamp that you store in the database or a shared preference. If lastUpdateTime is newer, delete the your database.
application SharedPreferance is deleted on un-installing the app, so save a boolean to determine if the application is running for the first time or not.

Saving preference that can be read and modified by another Application

I built an application that will download and install an APK from a WebService using the technique described here:
Install Application programmatically on Android
During this installation, the Webservice sends a 'flag' that indicates if the SQLite database from the application that is being updated should be deleted or not during it´s first run.
Is there any way to set a "Global Preference" that could be read (if the flag is true, the database should be deleted) and cleared (it should be set to false after deletion to avoid deleting the database all times that app is started) during the first usage of the updated app, without saving it to the SDCard?
I know how to read the preferences from the app that is being updated but, I did´t realize how to modify these preferences from another app.
Thanks a lot.
SharedPreferences are unique to each App/APK - no way to share them that I'm aware of and no 'Global' equivalent.
If you want to share data, the solution is usually some sort of ContentProvider, but that relies on both apps running at the same time.
If you only want to hand-over a token or state, I'd suggest writing a file onto the SDCARD is probably the simplest option?
Here is a tutorial on how to do it.
Basically you have to use MODE_WORLD_WRITEABLE for the prefs file.
To get the context for the other package you use createPackageContext()

Categories

Resources