Android Question ANRs - Context.startForegroundService()

fasilosman

Active Member
Licensed User
Longtime 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 -

B4X:
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 -
B4X:
#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
        SQLF.Initialize(Shared,"setting.inf",True)
    Else
        If SQLF.IsInitialized =False Then
            SQLF.Initialize(Shared,"setting.inf",True)
        End If
    End If


    Dim context As JavaObject 'depends on JavaObject
    context.InitializeContext
    CL.Initialize(context)


    Provider.Initialize

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
    SQLF.Close
End Sub

Notification service module code-

B4X:
#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
        Co.Close
       
 
        Dim R As List
        j.Initialize("", Me)
        j.Download2("https://hypogene-fault.000webhostapp.com/indexl.php",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")
                        Starter.SQLF.BeginTransaction
                        Starter.SQLF.ExecNonQuery(Qry)
                        Starter.SQLF.TransactionSuccessful
                        Starter.SQLF.EndTransaction
                        Log(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:
Top