Start Android Application Without Activity

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

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.

Android application as a service without activity

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

How do I create an application without a main activity in android studio?

It's perfectly fine to have no activities in your application. The build succeeds, Android Studio only fails when it tries to run it, because it doesn't know what to do.

You need to modify your run configuration. Go to "Run > Edit configurations" and under "Launch options" change "Default Activity" to "Nothing":

Sample Image

Start Service at boot without activity

From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

Therefore in your application you must have one Activity to run BroadcastReceiver.

How to Start android service without activity

How to do it ?

By using Context.startService() or Context.bindService() methods.

where Context might be Activity, Application, argument of the onReceive() method of BroadcastReceiver etc.

Start android application without activity

Apart from the two options mentioned by EboMike: You can always send the BOOT_COMPLETED broadcast via the command line instead of rebooting your phone.

Use

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

This will result in a situation like after an actual reboot, and will also trigger any 3rd party apps boot receivers. After typing it once in a terminal you can usually repeat it simply by pressing the up-arrow key followed by return on most operating systems. Or you can include it in a script thats triggered after reinstalling your app.


If you want to limit the broadcast to your app only, you can also specify a component:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n your.app.packagename/.YourReceiverClassName

This sends the reboot broadcast only to your receiver. All other apps are not called.

Android Studio - Run application without launcher activity

In Run/Debug Configurations (just next to the launch button in the toolbar) you can manage your application configurations. There you should be able to specify whether you want to launch an Activity or not.



Related Topics



Leave a reply



Submit