7

I want to launch a Service when the app is launched instead of an Activity; and then said Service will launch an Activity. I need to do this because my app needs to be running ALWAYS, and when I say ALWAYS I mean ALWAYS. And the only way I've managed to avoid the OS killing my app is by starting a service as Sticky and should Android kill either my Activity or my Service I'll restart them right away.

I found this question but the top answer seems rather clumsy, any one has a better idea?

PS: I know this doesn't look like a very friendly app but this is a very specific research scenario and it's not intended for regular users, i.e. the phone is solely used for this purpose; but even if memory is dedicated to my app Android keeps killing it every now and then... Any doubts I might have had about Android's purported strict memory management scheme are now gone.

Community
  • 1
  • 1
Moises Jimenez
  • 1,962
  • 3
  • 21
  • 43

1 Answers1

14

In general Activity does NOT have to show any UI - it usually does but it is NOT mandatory. So you can simply set app's starting point to your "invisible" activity. And invisible means either themed as

android:theme="@android:style/Theme.NoDisplay"

or simply your code will not do any setContentView() and once it's job is done in your onCreate(), you start another activity and terminate this one with finish() - and no UI would pop up from that activity - that way you can easily benefit from doing your job in activity subclass (which may be simpler for some tasks) and still do not need any UI:

public void onCreate(Bundle bundle) {
   super.onCreate(bundle);

   // [... do your job here...]

   // we're done, so let's jump to another acitivity
   // this can be skipped if you do not want to jump anywhere

   Intenet intent = new Intent(....)
   ...

   try {
      startActivity( intent );

      // finish him
      finish();

   } catch ( Exception e ) {
      e.printStackTrace();
   }

}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    But then it IS mandatory to start an Activity? Can't skip that step? – Moises Jimenez Oct 10 '12 at 10:41
  • 3
    @rodkarom: Launcher icons are always tied to activities. Hence, if you want a launcher icon to trigger a service, the launcher icon needs to start an activity that triggers the service. This, of course, assumes that by "when the app is launched" you mean "when the user taps on a launcher icon", as "apps" are not "launched" in Android. – CommonsWare Oct 10 '12 at 10:44
  • The trick is that to launch the app system will invoke that element which got right `intent-filter` attached to it. Theoretically, as intenet-filter can be assigned to activity, service or broadcast, any object of that type could be launched (however I did not tried anything beside activity for Launcher's intents). Still I see no problem launching activity without UI just to do my setup job. – Marcin Orlowski Oct 10 '12 at 10:44
  • I figured I could do something like that but with a service, by adding an android.intent.action.MAIN action to the service's intent-filter in the Manifest but it does not work. That's why I asked. – Moises Jimenez Oct 10 '12 at 10:48
  • 1
    the fact intent-filter can be assigned to more than one type of object does not mean these objects can be swapped. PackageManager can check what **activity** can handle certain Intent (which means if your app declares Service instead, it will not be found as Service is not Activity). Use "invisible" activity or set `Broadcast Receiver` and fire Service from here. – Marcin Orlowski Oct 10 '12 at 10:52