Launch Android application without main Activity and start Service on launching application
AndroidAndroid ActivityAndroid ServiceAndroid Problem Overview
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.
Android Solutions
Solution 1 - Android
You said you didn't want to use a translucent Activity, but that seems to be the best way to do this:
- In your Manifest, set the Activity theme to
Theme.Translucent.NoTitleBar
. - Don't bother with a layout for your Activity, and don't call
setContentView()
. - In your Activity's
onCreate()
, start your Service withstartService()
. - 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.
Solution 2 - Android
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.
Solution 3 - Android
The reason to make an App with no activity or service could be making a Homescreen Widget app that doesn't need to be started.
Once you start a project don't create any activities. After you created the project just hit run. Android studio will say No default activity found
.
Click Edit Configuration (From the Run menu) and in the Launch option part set the Launch value to Nothing.
Then click ok and run the App.
> NOTE: Since the app has no launcher activity, no icon will be shown in the apps menu. Instead, look for it in device's App manager.
Solution 4 - Android
Android Studio Version 2.3
You can create a Service without a Main Activity by following a few easy steps. You'll be able to install this app through Android Studio and debug it like a normal app.
First, create a project in Android Studio without an activity. Then create your Service class and add the service to your AndroidManifest.xml
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name="com.whatever.myservice.MyService">
<intent-filter>
<action android:name="com.whatever.myservice.MyService" />
</intent-filter>
</service>
</application>
Now, in the drop down next to the "Run" button(green arrow), go to "edit configurations" and within the "Launch Options" choose "Nothing". This will allow you to install your Service without Android Studio complaining about not having a Main Activity.
Once installed, the service will NOT be running but you will be able to start it with this adb shell command...
am startservice -n com.whatever.myservice/.MyService
Can check it's running with...
ps | grep whatever
I haven't tried yet but you can likely have Android Studio automatically start the service too. This would be done in that "Edit Configurations" menu.