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?
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.
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).
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.
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.
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.
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:
@android:style/Theme.NoDisplay
under Theme tag for this Activity in AndroidManifest.xml.
onCreate()
of your Activity. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With