Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch service from app start, not activity

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.

like image 748
Moises Jimenez Avatar asked Oct 10 '12 10:10

Moises Jimenez


People also ask

Can we start a service without activity?

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to. The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher.

Can you start an activity from a service?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

How we can start activity and service in Android?

Start a service. An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. // use this to start and trigger a service Intent i= new Intent(context, MyService. class); // potentially add data to the intent i.

What is startActivity in Android?

Starting activities or services. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.


1 Answers

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();
   }

}
like image 119
Marcin Orlowski Avatar answered Sep 30 '22 12:09

Marcin Orlowski