104

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.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
Meher
  • 2,545
  • 3
  • 26
  • 53
  • have a look on to this [question][1] [1]: http://stackoverflow.com/questions/4468006/can-i-start-a-service-without-activity-or-receiver – Munish Kapoor Jun 06 '12 at 07:18

4 Answers4

136

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.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • 2
    it gives me blank screen after launching my application.which shd not be the case... – Meher Jun 06 '12 at 07:20
  • 1
    @meher I just tested it and it worked perfectly for me -- my Service started and I didn't see any Activity, even for an instant. Please post the content of your Activity's onCreate(), and we should be able to work out what's wrong. – Darshan Rivka Whittle Jun 06 '12 at 07:33
  • when i installed my application for first time i can see the activity launched and disappering in a second. – Meher Jun 06 '12 at 09:07
  • public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i("activity", "starting service"); startService(new Intent(TestActivity.this,MyService.class)); finish(); } } – Meher Jun 06 '12 at 09:08
  • @meher Okay, I see it now -- I happened to have a translucent theme in the Activity I was testing and forgot that. I've updated my answer to reflect this. (Why don't you want to use a translucent theme?) Also, I've added what I think is an important point -- please indicate to your users, one way or another, that you are doing something when they press the launcher icon. – Darshan Rivka Whittle Jun 06 '12 at 10:02
  • In my application,there is no ui and no launcher.its just service does the my job using broadcast receivers.can't make it done without using activity ? ya i have seen Translucent activity theme as i dont require activity or i dont want to launch activity.I just want my service to run. – Meher Jun 06 '12 at 12:14
  • @meher I understand, but I thought you still wanted to have a launcher icon so that your users can start the Service. (For that matter, you'll probably want to give them a way to stop the Service, as well.) – Darshan Rivka Whittle Jun 06 '12 at 13:23
  • @DarshanComputing Shouldn't you use startService instead of bindService? When the Activity exists, won't the bound-only service be terminated? – David Doria Oct 03 '13 at 13:16
  • @DavidDoria I think you're right: `startService()` makes more sense here. (I'm not sure if `bindService()` would cause the Service to be terminated when the Activity finishes; I think as long as you don't unbind, it might keep running. Either way, there's no reason to bind here.) – Darshan Rivka Whittle Oct 03 '13 at 18:43
  • 1
    I realize this is an old question, but it doesn't appear to function any longer. Changing the main activity theme to Theme.Translucent.NoTitleBar or Theme.Translucent.NoTitleBar.FullScreen causes an otherwise working application that briefly shows the activity screen into one that always causes an "application stopped responding" message. – alzee Jul 28 '15 at 14:00
36

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.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
24

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.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
  • 2
    Thanks, that was exactly my case. In case you don't see Edit Configuration like I did, select **Run > Run...** from the main menu and then select **Edit Configurations...**. – pm_ May 06 '20 at 07:48
  • 1
    Also, the application class doesn't start – Maor Hadad Mar 29 '21 at 13:24
7

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Merc
  • 393
  • 5
  • 10
  • 1
    I get following error from adb shell: Starting service: Intent { cmp=com.example.wolk.myapp/.MyIntentService } Error: Not found; no service started. LOGCAT shows: ActivityManager: don't allow to start ServiceInfo{274d8af com.example.wolk.myapp.MyIntentService – Trismegistos Jul 18 '18 at 23:15