Android Code Snippet How to determine what started a Service


Well-Known Member
Licensed User
As far as I can tell, there are three different reasons a service was started:
  1. Manually started using StartService
  2. Scheduled start using StartServiceAt or StartServiceAtExact
  3. Automatically started at boot using #StartAtBoot

I recently found myself needing to know why a service was started, so I came up with the snippet below. Hopefully it can help somebody else.

If StartingIntent.Action = "android.intent.action.BOOT_COMPLETED" Then
    ToastMessageShow("Started because device booted", False)
Else If Bit.And(StartingIntent.Flags, 4) = 4 Then
    ToastMessageShow("Started from background", False)
    ToastMessageShow("Started manually", False)
End If