I have the following scenario in my application. There is no UI in my application; instead there is a Service which starts on boot up and will continuously run.
How can I configure my manifest file without a main Activity? Can I launch my app without any Activity? And on launching my app, my Service should start. Is this possible?
I don't want to make a translucent Activity to start the 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.
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 .
onCreate(Bundle) method is the place, where the user initializes the activity. This method is called when the activity is starting. This is the method that is used to initialize most of the things in the android app.
Therefore, unlike apps on most other systems, Android apps don't have a single entry point (there's no main() function). Because the system runs each app in a separate process with file permissions that restrict access to other apps, your app cannot directly activate a component from another app.
You said you didn't want to use a translucent Activity, but that seems to be the best way to do this:
Theme.Translucent.NoTitleBar
.setContentView()
.onCreate()
, start your Service with startService()
.finish()
once you've started the Service.In other words, your Activity doesn't have to be visible; it can simply make sure your Service is running and then exit, which sounds like what you want.
I would highly recommend showing at least a Toast notification indicating to the user that you are launching the Service, or that it is already running. It is very bad user experience to have a launcher icon that appears to do nothing when you press it.
Yes you can do that by just creating a BroadcastReceiver
that calls your Service
when your Application boots. Here is a complete answer given by me.
Android - Start service on boot
If you don't want any icon/launcher for you Application you can do that also, just don't create any Activity with
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
Just declare your Service
as declared normally.
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