Is there on install event in android?
Asked Answered
N

4

51

Is there some event/receiver or something for handling first execution after installation or directly after installation? Or Do I need it emulate with preferences?

Neuberger answered 9/2, 2010 at 8:7 Comment(1)
I am writing a service. It has no GUI. How can I schedule the service to run daily? (It seems that I cannot, because it seems that I cannot execute code directly after execution.) It will only be installed manually, so adb could be used, but this seems unnecessary.Theotokos
T
53

There is the ACTION_PACKAGE_ADDED Broadcast Intent, but the application being installed doesn't receive this.

So checking if a preference is set is probably the easiest solution.

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true);
p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();
Tharp answered 9/2, 2010 at 8:17 Comment(2)
Thanks a lot! I was looking for that intent, I have a Service that must have up to date knowledge of the installed packets every sec, with this intent I can cache it until a change occurs.Indicant
This actually does not answer the question. The answer only shows how to conditionally execute parts of the code. However, it still requires that the code (namely the if-condition) is executed in the first place. The approach above is fine for the main activity of the app and can be placed in its onCreate callback. Still, this requires the user to start the app at least once. But where do you put this code if you do not have an activity or if you cannot rely on the user to start the activity?Angadresma
N
4

See Get referrer after installing app from Android Market - you can put whatever you want in there. I believe this is how Plan B works - the app that can send back your phone's location after it's stolen, that you install from the website after it's been stolen.

Nachison answered 8/3, 2012 at 14:18 Comment(0)
L
1

I don't think there is such a thing, and I don't think this would be a good idea : usually you have to handle not only installations but some updates (say : a new version with features) or the proper initialization of some resources.

For the resources, the best way is to check them directly.

For the version, I use the database, it's so easy.

Lotson answered 9/2, 2010 at 8:13 Comment(1)
I've given motivation (i.e., a reason why this would be a good idea) above: #2228104Theotokos
R
1

The SQLiteOpenHelper's OnUpgrade method is called when the database version changed. I suppose this could be used to do other things than just handling the new schema.

Retribution answered 27/6, 2010 at 13:42 Comment(2)
Sorry for necroposting, but i believe there is only proper thing to do there -- handling new schema. Refering to SOLID's Single Responsobility Principle and common sence. Just in case of some one will see it.Naturalize
This will be called every time the database version changes, so not a good place to do this.Tetraploid

© 2022 - 2024 — McMap. All rights reserved.