Android Code Snippet How to determine what started a Service

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.

B4X:
If StartingIntent.Action = "android.intent.action.BOOT_COMPLETED" Then
    ToastMessageShow("Started because device booted", False)
Else If Bit.And(StartingIntent.Flags, 4) = 4 Then
    ' https://developer.android.com/reference/android/content/Intent#FLAG_FROM_BACKGROUND
    ToastMessageShow("Started from background", False)
Else
    ToastMessageShow("Started manually", False)
End If
 
Top