4

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

Community
  • 1
  • 1
Snake
  • 14,228
  • 27
  • 117
  • 250
  • proper way is here https://stackoverflow.com/questions/13035244/wake-an-application-after-market-update?noredirect=1&lq=1 – Prabhu M Oct 04 '17 at 13:41

2 Answers2

1

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.

yoah
  • 7,180
  • 2
  • 30
  • 30
  • Shared pref is cleared when device is restarted. I don't want my code run every time the device is restarted – Snake Jan 29 '13 at 15:02
  • 1
    If by restart you mean reboot then no, shared prefs are NOT cleared on reboot. – yoah Jan 29 '13 at 19:46
0

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.

Nimish Choudhary
  • 2,048
  • 18
  • 17
  • The problem that shared pref is cleared when device is restarted. However db file is not. How do I handle this – Snake Jan 29 '13 at 15:01
  • Will it delete on restart? check implementation. I don't see them getting disappeared. check implementation. – Nimish Choudhary Jan 29 '13 at 17:40
  • I did a test and apperently when I reboot my device, force close an app, or clear the cache,then the shared pref is wiped out. It is totally not reliable and I always avoid using it at all costs – Snake Jan 30 '13 at 14:21
  • well why do you want to clear the cache, its similar to uninstalling the app and then reinstalling? will clearing the cache clear the DB too ? – Nimish Choudhary Jan 31 '13 at 05:36
  • Clearing the cache does not clear DB. I just dont want people to clear cache by mistake and my code runs again – Snake Jan 31 '13 at 06:36