Android application as a service without activity
Asked Answered
A

1

39

I am making a set of apps and I have pretty much the same background service for all of them.

I'm trying to make an app that has only this Service. so I don't repeat it in all of them, but the thing is don't need any Activity. because there is no UI needed for it, and so the user can't close it except if they stop the Service.

I tried to remove the Activity, but then the app doesn't run or start. My question is: can I make an app exactly like Google Play Services so other apps can use its Service.

If yes than a snippet or a sample would be very welcome.

Aboriginal answered 19/6, 2014 at 13:9 Comment(2)
Create your Service in a library project. It's not possible to have a Service on its own as a stand-alone "app". It needs to be started manually by a user through an Activity.Gelation
You can not develop any app with only service.Placard
E
39

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. If there's no activity, you'll have to find another way to start it.

It is easy to do, though. Just fire an intent from one of your other programs, like this:

startService(new Intent("my.service.intent"));

... where the service is registered your manifest, like this:

        <service android:name=".SomeService" >
          <intent-filter>
            <action android:name="my.service.intent"/>
          </intent-filter>

You could use that intent to pass Parcelable parameters to the service, and the service can reply by broadcasting intents back.

Of course startService and broadcastIntent are a bit clunky if you really need a complex API between applications and your service. If you need something richer, you will want to look into AIDL and a Bound Service.

Edited to add Intent Filter

Eisenach answered 19/6, 2014 at 13:59 Comment(7)
Thank you for your answer, it was very useful. I actually was wondering how to call the service. I add a BroadcastReceiver to the service(app) and I made it so it start at the boot, this should solve the fire event for the service to start.Aboriginal
@ilyas Yep, that should work. Remember, though, that just because you start it at boot does not mean it will stay running. Android will, eventually, kill it. Also, you can minimize battery use, by running your service only when it is actually necessary...Eisenach
how can I do this in Android Studio? I doesn't allow to create an application without an activity...Staw
I don't see how this solves the issue, you're still starting the background service from an activity, just from an activity in that application.Catgut
@MattD What makes you think it has to be in the same application?Eisenach
Sorry I mean, "just not from an activity in that application". I assume OP, as was I, was looking for a way to start the service at startup without the user opening any app at all. I found a way using broadcast receivers though.Catgut
What makes you think it has to be an Activity? ... and, sure, the most common way to start an app, wo/ user interaction, is by catching BOOT_COMPLETE with a Receiver. The Receiver starts the Service...Eisenach

© 2022 - 2024 — McMap. All rights reserved.