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?

Dmytro
  • 2,200
  • 3
  • 20
  • 32
  • 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. – user2768 Nov 12 '15 at 09:53

4 Answers4

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();
Shriken
  • 140
  • 2
  • 9
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • 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. – Andras Balázs Lajtha Feb 15 '12 at 07:40
  • 4
    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? – user2690527 Oct 29 '18 at 12:41
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.

Community
  • 1
  • 1
Kenton Price
  • 5,229
  • 4
  • 23
  • 21
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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I've given motivation (i.e., a reason why this would be a good idea) above: http://stackoverflow.com/questions/2227604/is-there-on-install-event-in-android#comment55110087_2227604 – user2768 Nov 12 '15 at 09:55
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.

cdonner
  • 37,019
  • 22
  • 105
  • 153
  • 6
    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. – ryabenko-pro Dec 04 '12 at 14:07
  • This will be called every time the database version changes, so not a good place to do this. – dramzy Dec 01 '15 at 16:02