Can I use CallSub in a widget?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I tried to use CallSub to call a sub routine from inside a widget but I seem to be missing something because the toast message in the sub routine called is not being displayed.

B4X:
Sub ButtonMasterControl_Click

   CallSub(Main, "ToggleMasterControlButton")
   
   If IsPaused(ChimeService) Then

      rv.SetText("ButtonMasterControl", "Enabled")
   Else

      rv.SetText("ButtonMasterControl", "Disabled")
   End If   
End Sub

Could you correct my coding so it will work?

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Thanks for the replies.

I will look for a tutorial on persistent storage.

I need to use a way to signal a service about the button state (enabled/disabled). Can I stop or re-start the other service directly from in the widget?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I changed the code in the widget like this but the button label is not changing:

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

      Dim rv As RemoteViews
End Sub
Sub Service_Create
   
   ' Configure the widget.
   '----------------------
   rv = ConfigureHomeWidget("widget", "rv", 0, "Leiman", True)
End Sub

Sub Service_Start (StartingIntent As Intent)

   If IsPaused(ChimeService) Then
      rv.SetText("ButtonMasterControl", "Disabled")
   Else
      rv.SetText("ButtonMasterControl", "Enabled")
   End If   

   If rv.HandleWidgetEvents(StartingIntent) Then 
      Return
   End If
End Sub

Sub Service_Destroy

End Sub

Sub rv_RequestUpdate
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    StopService("")
End Sub

Sub ButtonMasterControl_Click

   If IsPaused(ChimeService) Then

      StartServiceAt(ChimeService, DateTime.Now, True)
      rv.SetText("ButtonMasterControl", "Enabled")
   Else

      StopService(ChimeService)
      rv.SetText("ButtonMasterControl", "Disabled")
   End If   
End Sub

I placed a toast message in the other service and that works showing it's being enabled and disabled.

I'm not sure why the button text is not changing in ButtonMasterControl_Click. Am I using the correct syntax for SetText? The button is correct in Service_Start if I delete the widget and place it back on the home screen.
 
Last edited:
Upvote 0
Top