53

I am creating an application whose only component is a service which keeps on running in background (basically a proxy server) but I am not able to find a way how to start that service. Application can not have any UI or user interaction so I am not using Activity.
Broadcast receiver can listen to BOOT broadcast but how do I start service first time when it is installed and how can I keep it running? or is there a broadcast which I can listen after app is installed e.g. may be TIME_TICK but that has to be registered from activity I think.

Community
  • 1
  • 1
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • 1
    I haven't tried this myself but would it be possible to start a service from the Application object? – phreed Mar 21 '11 at 12:27
  • I think it works http://android-codes-examples.blogspot.com/2011/11/running-service-in-background-on.html – Android Nov 22 '11 at 06:48

3 Answers3

45

Unfortunately right now there is no reliable way to receive a broadcast event after your applicaiton has been installed, the ACTION_PACKAGE_ADDED Intent does not broadcast to the newly installed package.

You will have to have a broadcast receiver class as well as your service in order to receive the ACTION_BOOT_COMPLETED event. I would also recommend adding the ACTION_USER_PRESENT intent to be caught by that broadcast receiver, this requires Android 1.5 (minSDK=3), this will call your broadcast receiver whenever the user unlocks their phone. The last thing that you can do to try to keep your service running without having it easily be shut down automatically is to call Service.setForeground() in your service onCreate to tell Android that your service shouldn't be stopped, this was added mainly for mp3 player type services that have to keep running but can be used by any service.

Make sure you add the proper permissions for the boot_complete and user_present events in you manifest.

Here is a simple class that you can use as a broadcast receiver for the events.

package com.snctln.util.WeatherStatus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class WeatherStatusServiceReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getAction() != null)
        {
            if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
                intent.getAction().equals(Intent.ACTION_USER_PRESENT))
            {
                context.startService(new Intent(context, WeatherStatusService.class));
            }
        }
    }
};

Good luck.

snctln
  • 12,175
  • 6
  • 45
  • 42
  • 7
    Good advice. One thing about Service.setForeground(): it no longer works in Android 2.0+. All "long-lived" services are now required to have a persistent notification in the notification area while they're running. Example here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.html – Jarett Millard Dec 24 '09 at 23:45
  • 2
    ACTION_USER_PRESENT is an awesome tip. I've been struggling with keeping my service alive for ages, and this just solved it. – defrex Nov 24 '10 at 15:51
  • 5
    Found out, that from 3.0 upwards one needs to start the application at least once to enable getting the intent for it - weird, but works for me. That means, that one has to create a little activity (i.e. with introductory stuff) which is started after installation. After that you will receive the intents properly. – ubuntudroid Jan 12 '12 at 15:08
  • is that documented anywhere ubuntudroid? – Archimedes Trajano Feb 07 '12 at 00:52
  • 3
    @ArchimedesTrajano I know this question is old but it was the first result on Google. There is some documentation [here](http://developer.android.com/about/versions/android-3.1.html#launchcontrols) and a good explanation [here](http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html). – jpalm Jun 21 '13 at 16:12
16

Return START_STICKY in Service.onStartCommand

Android will restart your service

Piyush
  • 18,895
  • 5
  • 32
  • 63
shaobin0604
  • 1,179
  • 12
  • 14
1

I'm researching on the same issue. Here's is what I've found: http://kfb-android.blogspot.de/2009/04/registering-for-timetick-after-reboot.html

That simple little demo shows a solution by playing a little bit of ping-pong between a BroadcatReceiver and a Service. The Receiver is registered to start at boot time by registering in the Manifest to receive ACTION_BOOT_COMPLETED. It just starts a service when receviving ACTION_BOOT_COMPLETED. The Service in turn registeres the BroadcastReceiver then for the ACTION_TIME_TICK.

The app logic will be implemented in the BroadcastReceiver.

jboi
  • 11,324
  • 4
  • 36
  • 43