Android Question Background service is unable to execute method from Main.b4a

IamTrying

Active Member
Licensed User
1. In Main.b4a i have following:

B4X:
Sub sendSMS
    Log(">>> JOB SMS: " & myService.sms_id)
  
    If myService.working_on_sms = "" Then       
        myService.working_on_sms  = myService.sms_id
        Log(">>> JOB STARTED: " & myService.working_on_sms)
      
        ' Clear the SMS job
        job2.Initialize("job2", Me)
        Dim new_login As String = "id=" & myService.working_on_sms & "&action=done"
        Dim new_loginurl As String = "https://" & myService.myAgentID & ".do not show.com/app/smssent"
        job2.PostString(new_loginurl,new_login)
      
        ' SMS
        Dim Sms1 As PhoneSms
        Sms1.Send(myService.sms_to,myService.sms_text)
      
        ' WhatsAPP
        'Dim Intent1 As Intent
        'Intent1.Initialize(Intent1.ACTION_VIEW, $"https://api.whatsapp.com/send?phone=${myService.sms_to}&text=${myService.sms_text}"$)
        'StartActivity(Intent1)
      
        ' Dial
        'Dim skype As Intent
        '"skype:echo123?call&video=true"
        'skype.Initialize(skype.ACTION_VIEW, "skype:echo1234?chat&topic=skype_notify")
        'skype.SetComponent("com.skype.raider")
        'StartActivity(skype)
      
      
        myService.working_on_sms = ""
    Else
        Log(">>> JOB BUSY: " & myService.working_on_sms)  
                  
    End If
  
  
  
End Sub

2. In myService.bas i have following:

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "job1"
                'print the result to the logs
                parser.Initialize(Job.GetString)
                Dim root As Map = parser.NextObject
                Dim result As String = root.Get("result")
                Log(">>> HTTPS: " & result)
                If result = "ok" Then
                  
                    ' Do the SMS task
                    sms_id = root.Get("sms_id")
                    sms_status = root.Get("sms_status")
                    sms_to = root.Get("sms_to")
                    sms_text = root.Get("sms_text")
                                      
                    If sms_status = "pending" Then
                        Log(">>> sendSMS call")
                        CallSub(Main,"sendSMS")
                    End If
                  
                    ' Do OLD activity
                    Dim db As List = root.Get("db")
                    CallSub(Main,"clearList")
                    For Each coldb As Map In db
                        'Dim kiosk As String = coldb.Get("username")
                        Dim callstatus As String = coldb.Get("callstatus")
                        Dim callername As String = coldb.Get("callername")                  
                      
                        If callstatus = "orange" Then
                            html = callername
                            rowCallstatus = callstatus
                            CallSub(Main,"addList")
                            Notification1.Sound = True
                            Notification1.SetInfo("do not show: " & callername, "Agent: " & myUsername & ", " & "Ticking: " & myTicking , Main)
                            Notification1.AutoCancel = True
                            'Notification1.Notify(1)
                            Notification1.Vibrate = True

                            If IsPaused(Main) Then
                                'StartActivity("Main")
                                CallSub(Main, "SetShowWhenLocked")
                                StartActivity("Main")
                            Else
                                CallSub(Main, "SetShowWhenLocked")
                                StartActivity("Main")
                            End If
                          
                          

                        Else If callstatus = "red" Then
                            html = callername
                            rowCallstatus = callstatus
                            CallSub(Main,"addList")
                        Else
                            html = callername
                            rowCallstatus = callstatus
                            CallSub(Main,"addList")
                        End If
                      
                      
                      
                    Next
                  
                    CallSub(Main, "FinishDownload")
                  
                                  
                Else
                    Log("fail")
                  
                  
                    ' Do the SMS task
                    sms_id = root.Get("sms_id")
                    sms_status = root.Get("sms_status")
                    sms_to = root.Get("sms_to")
                    sms_text = root.Get("sms_text")
                                      
                    If sms_status = "pending" Then
                        CallSub(Main,"sendSMS")
                    End If
                  
                End If
  
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub

3. myService.bas is running as background service. But it is failing to call following:

B4X:
CallSub(Main,"sendSMS")

4. When App is running in background CallSub(Main,"sendSMS") fails but when the app is
active CallSub(Main,"sendSMS") works.


Q. How to make the CallSub(Main,"sendSMS") method work from the background service?
 

IamTrying

Active Member
Licensed User
Applied following but its still failing to call the sendSMS when app is in background.

B4X:
If sms_status = "pending" Then
                        Log(">>> sendSMS call")
                        'CallSub(Main,"sendSMS")
                        CallSubDelayed(Main,"sendSMS")
End If

Logs shows following:

B4X:
JobName = job1, Success = true
>>> HTTPS: ok
>>> sendSMS call
sending message to waiting queue (CallSubDelayed - sendSMS)
Ignoring event (too many queued events: CallSubDelayed - sendSMS)
 
Upvote 0

IamTrying

Active Member
Licensed User
Thanks.

FYI - To temporary quickly fix the issue i have putted the sub sendSMS inside myService.bas and its now working. But CallSubDelayed did not resolved the problem.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
But CallSubDelayed did not resolved the problem.
because you did not understand how b4A works.
It is not a tempory fix; it is the Solution.

In Main.b4a i have following:
job2.Initialize("job2", Me)
Dim new_login As String = "id=" & myService.working_on_sms & "&action=done"
Dim new_loginurl As String = "https://" & myService.myAgentID & ".do not show.com/app/smssent"
job2.PostString(new_loginurl,new_login)
You are starting the job in Main so you MUST expect the answer in MAIN too. Means the Jobdone is raised in Main.
 
Upvote 0
Top