Callsub and Service module

mrossen

Active Member
Licensed User
Longtime User
Hi I can not get my test app to call RefreshData in main activity when I get the expeted SMS.

The app will show the main activity but not execute RefreshData.

Am I doing something wrong with callsub?


B4X:
Sub smsint_MessageReceived (From As String, Body As String) As Boolean

   If From = "+45XXXXXXXX" Then 
    ToastMessageShow(From & " - " & Body, True)
    Main.SMSNumber = From
    Main.SMSBody = Body
    StartActivity(Main)
    CallSub(Main, "RefreshData")
    Return True
   End If

End Sub

Mogens
 

Attachments

  • SMS.zip
    7.1 KB · Views: 292

margret

Well-Known Member
Licensed User
Longtime User
Try this code and see if it shows Main as Paused. You are calling the sub right after StartActivity(Main) and it may not be running yet.

B4X:
Sub smsint_MessageReceived (From As String, Body As String) As Boolean
   If From = "+45XXXXXXXX" Then
      ToastMessageShow(From & " - " & Body, True)
      Main.SMSNumber = From
      Main.SMSBody = Body
      StartActivity(Main)
      If IsPaused(Main) = False Then
         CallSub(Main, "RefreshData")
      Else
         Msgbox("Main is Paused", "")
      End If
      Return True
   End If
End Sub
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

Yes, the activity seems to be paused. Is there a way to wait to call the sub until it is available.

Mogens
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that calling Msgbox from a Service will crash the application.

StartActivity sends a message to the message queue. When this message is processed the activity is started.
This means that if an activity is paused then calling StartActivity followed by CallSub will always fail. The activity will be paused at that time (unless it was not paused at all).

The best solution is:
B4X:
If IsPaused(Main) Then
 StartActivity(Main)
 RefreshDataFlag = True
Else
 CallSub(Main, "RefreshData")
End If

'Main activity
Sub Activity_Resume
 If ServiceModule.RefreshDataFlag = True Then
  ServiceModule.RefreshDataFlag = False
  RefreshData
 End If
End Sub
 
Upvote 0
Top