Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Android application without main Activity and start Service on launching application

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.

like image 795
Meher Avatar asked Jun 06 '12 07:06

Meher


People also ask

Can we start a service without activity in Android?

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.

Can we start service without activity?

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 .

Which method is defined for making an activity to be launched on Android application start?

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.

Why does an Android application has no main () function?

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.


2 Answers

You said you didn't want to use a translucent Activity, but that seems to be the best way to do this:

  1. In your Manifest, set the Activity theme to Theme.Translucent.NoTitleBar.
  2. Don't bother with a layout for your Activity, and don't call setContentView().
  3. In your Activity's onCreate(), start your Service with startService().
  4. Exit the Activity with 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.

like image 79
Darshan Rivka Whittle Avatar answered Sep 22 '22 08:09

Darshan Rivka Whittle


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.

like image 43
Lalit Poptani Avatar answered Sep 19 '22 08:09

Lalit Poptani