1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question ANRs - Context.startForegroundService()

Discussion in 'Android Questions' started by fasilosman, Jul 22, 2019.

  1. fasilosman

    fasilosman 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 -

    Firebase error report -

    Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{f714894 u0 teachislam.slschool/.notif}
           at android.app.ActivityThread$H.handleMessage + 1879(ActivityThread.java:1879)
           at android.os.Handler.dispatchMessage + 106(Handler.java:106)
           at android.os.Looper.loop + 214(Looper.java:214)
           at android.app.ActivityThread.main + 7076(ActivityThread.java:7076)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
           at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java: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(16As Boolean
    Public Provider As FileProvider

    End Sub

    Sub Service_Create

        Shared = rp.GetSafeDirDefaultExternal(
    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 StringAs 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 = 
    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 = 
                NOTID = Co.GetInt(
                FEEDID = Co.GetLong(
                QSID = Co.GetInt(
                FeedNot = Co.GetInt(
    End If
    Dim R As List
    "", Me)
    "https://hypogene-fault.000webhostapp.com/indexl.php",Array As String ("action""getlastqsid"))
            j.GetRequest.Timeout =
    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(
    If QSID <> ItemM.Get("ID"Then
    Dim n As NB6
    "997"Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(LoadBitmap(File.DirAssets,"applogo.png"))
    "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
    "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: Jul 22, 2019
  2. Erel

    Erel Administrator Staff Member Licensed User

    Not sure whether this is the cause of this error, however the starter service should never be started at boot.
  3. fasilosman

    fasilosman Active Member Licensed User

    OK. I will check on that
  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