Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch a Android Service when the app launches?

Tags:

android

I'm still fresh to Android and Id think the below config works for launching my service when the app launches.

    <service android:name=".PlaylistUpdaterService">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </service>

But this is not the case. What did I miss?

like image 977
Teo Choong Ping Avatar asked Dec 17 '10 04:12

Teo Choong Ping


People also ask

How do I trigger a 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.

How do I declare a service in manifest Android?

You declare a service in your app's Manifest, by adding a <service> element as a child of your <application> element. There's a list of attributes that you can use to control a service's behavior, but as a minimum you'll need to provide the service's name (android:name) and a description (android:description).

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.

How do I start an intent Service?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.


2 Answers

Wrong! extend the Application class (IE make your own), then in the onCreate() method do this.

//Service is below
Intent serviceIntent = new Intent(getApplicationContext(), PlaylistUpdaterService.class);
startService(serviceIntent);

And take that intent filter crap out of your declaration in the manifest file. Leave it as

<service android:name=".PlaylistUpdaterService">

The intent filter following needs to be in your home activity only

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> 

The reason you do this is because the Application class is started up as soon as the app is, and acts as kind of a global class the the android framework manages.

Actually if you want the service to run every time you go back to your home screen, you should start the service in your home classes onResume(). Putting it in the applications onCreate() will only start the service if the user is launching for the first time, or after the running process has been killed. Or you could put it in your home classes onCreate() but that is not even guaranteed to run every time.

like image 69
Danuofr Avatar answered Oct 07 '22 08:10

Danuofr


Correct me if I am wrong, but android.intent.category.LAUNCHER is valid only for Activity. So, does not look like valid way to start Service. The same you can achieve if you do the following:

  • create transparent Activity that will be used only to start Service
  • for that Activity, you do not need to specify GUI layout. So, you do not need to setContentView() in the activity's onCreate(). The only thing you need is to put

@android:style/Theme.NoDisplay

under Theme tag for this Activity in AndroidManifest.xml.

  • start Service from onCreate() of your Activity.
  • call finish() in onStart() of your Activity to close it.

So, your Activity will be invisible to the user, last shortly and nobody will notice that it was used to start the service.

like image 26
Zelimir Avatar answered Oct 07 '22 08:10

Zelimir