Android Question ANRs - Context.startForegroundService()


Active Member
Licensed User
I received the following ANRs & Crash report, couldn't figure out what is the error. Please help me.
I have a service to generate notifications. I hope that service module have a problem. I tested in an emulator and a real device but it is not generated in debug mode.

Heading of the ANR Clustor -

"Context.startForegroundService() did not then call Service.startForeground()"
Firebase error report -

Fatal Exception: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{f714894 u0 teachislam.slschool/.notif}
       at$H.handleMessage + 1879(
       at android.os.Handler.dispatchMessage + 106(
       at android.os.Looper.loop + 214(
       at + 7076(
       at java.lang.reflect.Method.invoke(
       at$ + 493(
       at + 965(

Starter Service Code -
#Region  Service Attributes
    #StartAtBoot: True
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals

    Public SQLF As SQL
    Public rp As RuntimePermissions
    Public Shared As String
    Public CL As Crashlytics
    Public OldIntent As Intent
    Public IconBatch As Map  
    Public IconCheck(16) As Boolean
    Public Provider As FileProvider

End Sub

Sub Service_Create

    Shared = rp.GetSafeDirDefaultExternal("islam")
    If Not(File.Exists(Shared,"setting.inf")) Then
        If SQLF.IsInitialized =False Then
        End If
    End If

    Dim context As JavaObject 'depends on JavaObject


End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy
End Sub
Notification service module code-

#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private AcNot As Boolean
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode= Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED

End Sub

Sub Service_Start (StartingIntent As Intent)
    StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, False)
    If StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") Then

        AcNot = False
        Dim Co As Cursor
        Dim NOTID As Int
        Dim FEEDID As Long
        Dim QSID As Int
        Dim FeedNot As Int
        Log("service started")
        Dim Rc As Map

        Co = Starter.SQLF.ExecQuery("Select * from Setting")
        If Co.RowCount > 0 Then
            Co.Position = 0
            NOTID = Co.GetInt("NOTID")
            FEEDID = Co.GetLong("FEEDID")
            QSID = Co.GetInt("QSID")
            FeedNot = Co.GetInt("FEEDNOT")
        End If
        Dim R As List
        j.Initialize("", Me)
        j.Download2("",Array As String ("action", "getlastqsid"))
        j.GetRequest.Timeout =60000
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            parser.Initialize(j.GetString )
            R = parser.NextArray
            Dim ItemM As Map
            Dim Catt As String
            If R.Size > 0 Then
                ItemM = R.Get(0)
                If QSID <> ItemM.Get("ID") Then
                    Dim n As NB6
                    n.Initialize("997", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(LoadBitmap(File.DirAssets,"applogo.png"))
                    n.Build("Teach Islam - Ask Expert", ItemM.Get("Q"), "tag1", Ask).Notify(997) 'It will be Main (or any other activity) instead of Me if called from a service.

                    Dim C As Cursor
                    c = Starter.SQLF.ExecQuery("Select * from setting")
                    If C.RowCount > 0 Then
                        Dim Qry As String
                        Qry="UPDATE Setting SET QSID = " & ItemM.Get("ID")
                    End If

                End If

            End If

        End If

        Log("service end")
        Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    End If
End Sub

Sub Service_Destroy

End Sub
Last edited: