Android Code Snippet How to determine what started a Service

Discussion in 'Code Snippets' started by Sandman, Jul 24, 2018.

Tags:
  1. Sandman

    Sandman 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.

    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice