0

I want to run a service in the background as long as the android device is on. I already have a broadcastreceiver listening for BOOT_COMPLETED to ensure it starts when it should, and it does. But when the main activity starts, I want to ensure that the service is still running and restart it if it's not. I'm fairly new to services to begin with so I'm not sure I'm doing it right.

In the manifest the service is defined as

<service
    android:name="MyService"
    android:icon="@drawable/myicon"
    android:label="MyServiceLabel"
    android:process=":my_process">
</service>

It's being started from the BroadcastReceiver using

public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, OgLogService.class);
    context.startService(service);
}

The meat of my service is

public int onStartCommand(Intent intent, int flags, int startId) {
    Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {
        synchronized public void run() {
            // call web service, do stuff
        }
    }, 0, 1000 * 60);
    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

Will this service stay on indefinitely or could the OS close it? If it's closed, how can my activity tell? If my activity does need to restart the service, do I need to do it in a special way to ensure that it's a separate process that won't die when the main activity dies? Sorry about having so many questions, I just feel a little misguided about services in their own processes (and services in general). Everything I read is about Services not in their own process.

I ask because at one point I believe I had the service running multiple times. The service and the activity don't interact directly. The service writes to the same DB that the activity reads from. At one point it appeared that information was duplicated in the database which I believe was caused by more than one instance of the service running at the same time.

I've tried using code from this answer but it doesn't seem to work for services running in different processes. In System Settings > Apps > Running, it shows my app running 1 process and 1 service (started by the broadcastreceiver) but the isMyServiceRunning method returns false and stepping through it seems to not list my service.

Community
  • 1
  • 1
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188

2 Answers2

1

There's no guarantee your service will stay. Basically you have to expect to be killed and then restarted or killed for good. Another thing, unless you are audio player you do NOT need to stay up all the time. If you need to do continuous work all the time then you probably forgot how limited mobile environment is (battery etc). If you need to so some repetitive tasks, then I suggest to go for AlarmManager to trigger the worker at given time or intervals and use IntentService to actually do the job.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • My service is purely to do something periodically the entire time the device is on. Would AlarmManager reliably do this? AlarmManager makes me think of alarms set by the clock, say to wake up in the morning. Does it have anything to do with those alarms? – Corey Ogburn Apr 04 '13 at 18:31
  • Yes it is - you just got the name wrong. It's basically for that purpose. Alarm is event driven by time. Like "run me each 5 minutes", or "run me at 15:40". So your boot receiver shall set the first alarm and then the alarm handling code shall set the next one (or you just set it once as recurring alarm - depends on what you need). See the docs on AlarmManager on http://developer.android.com/reference/android/app/AlarmManager.html – Marcin Orlowski Apr 04 '13 at 18:35
0

Here is a link to schedule your activity http://android-er.blogspot.in/2011/05/using-alarmmanager-to-start-scheduled.html Its good example, where you can cancel your pending intent whenever you want to cancel your service.

surender8388
  • 474
  • 3
  • 12