Android Question Don't understand the event flow of a widget

pricciardi

Member
Licensed User
Hi everyone,
sorry for my english. I am helping myself with google translator.

For educational purposes, I'm trying to write a widget that reads a Json file and shows what hypothetical users can give to the garbage collection service.
If it worked I would also try to publish it in the play store but this is not the question.
As you can read from line 23, the widget should update the text every 30 minutes but it simply remains impassive and does not update. Could someone explain to me where am I wrong? I'm wasting a lot of time on it. This is my first exercise with b4a.
For completeness I have attached the export of the project from b4a.

Thank you





Full Starter Service of the widget:
#Region  Service Attributes
    #StartAtBoot: True
    '#ExcludeFromLibrary: True
        
#End Region

Sub Process_Globals
    
    Log ("Process_Globals " & DateTime.Date(DateTime.now))
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim ObjRV As RemoteViews
    
End Sub


Sub Service_Create
    
    DateTime.DateFormat="hh:mm:ss:mm"
    'This is the program entry point.
    Log ("Service_Create " & DateTime.Date(DateTime.now))
    'This is a good place to load resources that are not specific to a single activity.
    ObjRV = ConfigureHomeWidget("main", "ObjRV", 30,"Cosa Butto Oggi", True)
    

End Sub

Sub Service_Start (StartingIntent As Intent)
    
    Log ("Service_Start " & DateTime.Date(DateTime.now))
    
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED" Then Return
    
    
    If ObjRV.HandleWidgetEvents(StartingIntent) = False Then
    
        'prendo il Jason con il calendario degli scarichi
        'Wait For(DownloadCalendario) Complete (calendarioSemplificato As String)   
        ObjRV_RequestUpdate '(calendarioSemplificato)
        'Sleep (0)
        
        
    End If
    
    'ObjRV_RequestUpdate
    
    
End Sub

Sub Service_TaskRemoved
    
    'This event will be raised when the user removes the app from the recent apps list.
    Log ("Service_TaskRemoved " & DateTime.Date(DateTime.now))
    
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
    
    Log (" Application_Error " & DateTime.Date(DateTime.now))
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub ObjRV_RequestUpdate '(calendario As String)
    
    Log ("ObjRV_RequestUpdate " & DateTime.Date(DateTime.now))
    Wait For(DownloadCalendario) Complete (calendarioSemplificato As String)
    AggiornaContentutoWidget (calendarioSemplificato)
    ObjRV.UpdateWidget
    
End Sub


Sub ObjRV_Destroy
    
    Log ("ObjRV_Destroy " & DateTime.Date(DateTime.now))
    StopService("")
    
    
End Sub

Sub AggiornaContentutoWidget (calendario As String)
    
    Log ("AggiornaContentutoWidget " & DateTime.Date(DateTime.now))
    'Verifico che giorno della settimana è
    Dim giorno As String = getGiornoDellaSettimana
    
        
    'passo il calendario ad un parser Jason per estrarre i rifiuti che si raccolgono oggi
    Dim messaggio As String = decodeCalendario(calendario, giorno )
    
    Dim  dataOra  As String   
    dataOra = DateTime.Date(DateTime.now)
    
    'imposto il testo sul widget
    ObjRV.SetText("Label1", messaggio)' & Chr(10)  & DateTime.Now )
    ObjRV.SetText("Label2",  dataOra &  " Controlla sul sito della Patrimonio eventuali variazioni.")
    
    
End Sub

Sub ObjRV_Disabled
    
    Log ("ObjRV_Disabled " & DateTime.Date(DateTime.now))
    StopService("")

End Sub

Sub DownloadCalendario As ResumableSub
    
    Log ("DownloadCalendario " & DateTime.Date(DateTime.now))
    'scarico il jason dal sito aziendale
    
    Dim job As HttpJob
    Dim calendarioJson As String
    
    job.Initialize("", Me)
    job.Download([MY JSON STRING URL])
    Wait For (job) JobDone(job As HttpJob)
    
    If job.Success Then
        calendarioJson = job.GetString
    End If
    
    job.Release
    
    Return calendarioJson
    
End Sub

Sub getGiornoDellaSettimana As String
    
    Log ("getGiornoDellaSettimana " & DateTime.Date(DateTime.now))
    
    Dim days() As String = Array As String("","Domenica" , "Lunedì" , "Martedì", "Mercoledì" ,"Giovedì" ,"Venerdì"  ,"Sabato" )
    'Dim days() As String = Array As String("","Domani il servizio di raccolta non passa",  "dalle 20.00 di oggi alle 6.00 di domani puoi buttare l'organico" , "dalle 20.00 di oggi alle 6.00 di domani puoi buttare plastica e lattine di alluminio", "dalle 20.00 di oggi alle 6.00 di domani puoi buttare organico e sfalci" ,"dalle 20.00 di oggi alle 6.00 di domani puoi buttare l'indifferenziato",  "dalle 20.00 di oggi alle 6.00 di domani puoi buttare l'organico" , "dalle 20.00 di oggi alle 6.00 di domani puoi buttare carta e vetro")
    
    Dim oggi As Int = DateTime.GetDayOfWeek(DateTime.Now)
    Dim domani As Int = oggi + 1
    
    Dim giornoDellaSettimana As String = days(domani)
    
    Return giornoDellaSettimana
    
End Sub

Sub decodeCalendario (calendarioSettimanale As String, giornoDellaSettimana As String ) As String
    
    Log ("decodeCalendario " & DateTime.Date(DateTime.now))
    
    'estraggo l'rsu del giorno dato dal tracciato Json
    Dim parser As JSONParser
    Dim Map1 As Map
    Dim rsuDelGiorno As String
    
    parser.Initialize(calendarioSettimanale)
    Map1 = parser.NextObject
    
    rsuDelGiorno = Map1.Get(giornoDellaSettimana)
    
    Return rsuDelGiorno
    
End Sub
 

Attachments

  • CosaButtoOggi.zip
    383.6 KB · Views: 149

pricciardi

Member
Licensed User
Better to stop the automatic foreground mode after the download was completed. Otherwise the OS can kill the process before it completes.

Have you checked the logs? Do you see the Service_Start message?

Yes, this is the log and there is the Service_Start:


Full log:
Logger connesso a:  HUAWEI VOG-L29
--------- beginning of crash
--------- beginning of main
--------- beginning of system
Process_Globals 03/25/2021
*** Service (starter) Create ***
Service_Create 01:06:33:06
** Service (starter) Start **
Service_Start 01:06:33:06
ObjRV_RequestUpdate 01:06:33:06
DownloadCalendario 01:06:33:06
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
AggiornaContentutoWidget 01:06:34:06
getGiornoDellaSettimana 01:06:34:06
decodeCalendario 01:06:34:06
** Receiver (starter) OnReceive **
The Starter service should never be started from a receiver.
** Service (starter) Start **
Service_Start 01:06:34:06
ObjRV_RequestUpdate 01:06:34:06
DownloadCalendario 01:06:34:06
AggiornaContentutoWidget 01:06:34:06
getGiornoDellaSettimana 01:06:34:06
decodeCalendario 01:06:34:06
** Activity (main) Pause, UserClosed = true **
** Receiver (starter) OnReceive **
The Starter service should never be started from a receiver.
** Service (starter) Start **
Service started in foreground mode.
Service_Start 01:06:43:06
** Receiver (starter) OnReceive **
The Starter service should never be started from a receiver.
** Service (starter) Start **
Service started in foreground mode.
Service_Start 01:06:43:06
ObjRV_Disabled 01:06:43:06
** Service (starter) Destroy (ignored)**
** Receiver (starter) OnReceive **
The Starter service should never be started from a receiver.
*** Service (starter) Create ***
Service_Create 01:07:00:07
** Service (starter) Start **
Service started in foreground mode.
Service_Start 01:07:00:07
ObjRV_RequestUpdate 01:07:00:07
DownloadCalendario 01:07:00:07
** Receiver (starter) OnReceive **
The Starter service should never be started from a receiver.
** Service (starter) Start **
Service started in foreground mode.
Service_Start 01:07:00:07
ObjRV_RequestUpdate 01:07:00:07
DownloadCalendario 01:07:00:07
** Receiver (starter) OnReceive **
The Starter service should never be started from a receiver.
** Service (starter) Start **
Service started in foreground mode.
Service_Start 01:07:00:07
ObjRV_RequestUpdate 01:07:00:07
DownloadCalendario 01:07:00:07
AggiornaContentutoWidget 01:07:00:07
getGiornoDellaSettimana 01:07:00:07
decodeCalendario 01:07:00:07
AggiornaContentutoWidget 01:07:00:07
getGiornoDellaSettimana 01:07:00:07
decodeCalendario 01:07:00:07
AggiornaContentutoWidget 01:07:00:07
getGiornoDellaSettimana 01:07:00:07
decodeCalendario 01:07:00:07


Now I changed the Service.StopAutomaticForeground position...
 
Upvote 0

pricciardi

Member
Licensed User
There must be some phone service that stops the widget. It works for an indefinite period of time, then it stops.
Sometimes the app icon shows a dot and also it show a notification with the name of the widget but without any text description.
This is the latest version of the service. I'll keep beating my head but now I want it to work!

Starter Service:
#Region  Service Attributes
    #StartAtBoot: True
    '#ExcludeFromLibrary: True
        
#End Region

Sub Process_Globals
    
    Log ("Process_Globals " & DateTime.Date(DateTime.now))
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim ObjRV As RemoteViews
    
End Sub


Sub Service_Create
    
    
    DateTime.DateFormat="hh:mm:ss:mm"
    Log ("Service_Create " & DateTime.Date(DateTime.now))
    'This is a good place to load resources that are not specific to a single activity.
    ObjRV = ConfigureHomeWidget("main", "ObjRV", 30,"Cosa Butto Oggi", True)
    

End Sub

Sub Service_Start (StartingIntent As Intent)
    
    Log ("Service_Start " & DateTime.Date(DateTime.now))
    
    'Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED" Then Return
    
    
    If ObjRV.HandleWidgetEvents(StartingIntent) = False Then
    
        ObjRV_RequestUpdate
            
    End If
    
    
End Sub

Sub Service_TaskRemoved
    
    'This event will be raised when the user removes the app from the recent apps list.
    Log ("Service_TaskRemoved " & DateTime.Date(DateTime.now))
    
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
    
    Log (" Application_Error " & DateTime.Date(DateTime.now))
    Log ("StackTrace: " & StackTrace)
    Log ("Exception: " & Error)
    
    Return True
    
End Sub

Sub Service_Destroy

End Sub

Sub ObjRV_RequestUpdate '(calendario As String)
    
    Log ("ObjRV_RequestUpdate " & DateTime.Date(DateTime.now))
    Wait For(DownloadCalendario) Complete (calendarioSemplificato As String)
    AggiornaContentutoWidget (calendarioSemplificato)
    ObjRV.UpdateWidget
    
End Sub


Sub ObjRV_Destroy
    
    Log ("ObjRV_Destroy " & DateTime.Date(DateTime.now))
    StopService("")
    
    
End Sub

Sub AggiornaContentutoWidget (calendario As String)
    
    Log ("AggiornaContentutoWidget " & DateTime.Date(DateTime.now))
    'Verifico che giorno della settimana è
    Dim giorno As String = getGiornoDellaSettimana
    
        
    'passo il calendario ad un parser Jason per estrarre i rifiuti che si raccolgono oggi
    Dim messaggio As String = decodeCalendario(calendario, giorno )
    
    Dim  dataOra  As String   
    dataOra = DateTime.Date(DateTime.now)
    
    'imposto il testo sul widget
    ObjRV.SetText("Label1", messaggio)' & Chr(10)  & DateTime.Now )
    ObjRV.SetText("Label2",  dataOra &  " Controlla sul sito della Patrimonio eventuali variazioni.")
    
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    
End Sub

Sub ObjRV_Disabled
    
    Log ("ObjRV_Disabled " & DateTime.Date(DateTime.now))
    StopService("")

End Sub

Sub DownloadCalendario As ResumableSub
    
    Log ("DownloadCalendario " & DateTime.Date(DateTime.now))
    'scarico il jason dal sito aziendale
    
    Dim job As HttpJob
    Dim calendarioJson As String
    
    job.Initialize("", Me)
    job.Download([JSON URL])
    Wait For (job) JobDone(job As HttpJob)
    
    If job.Success Then
        calendarioJson = job.GetString
    End If
    
    job.Release
    
    Return calendarioJson
    
End Sub

Sub getGiornoDellaSettimana As String
    
    Log ("getGiornoDellaSettimana " & DateTime.Date(DateTime.now))
    
    Dim days() As String = Array As String("","Domenica" , "Lunedì" , "Martedì", "Mercoledì" ,"Giovedì" ,"Venerdì"  ,"Sabato" )
    
    Dim oggi As Int = DateTime.GetDayOfWeek(DateTime.Now)
    Dim domani As Int = oggi + 1
    
    Dim giornoDellaSettimana As String = days(domani)
    
    Return giornoDellaSettimana
    
End Sub

Sub decodeCalendario (calendarioSettimanale As String, giornoDellaSettimana As String ) As String
    
    Log ("decodeCalendario " & DateTime.Date(DateTime.now))
    
    'estraggo l'rsu del giorno dato dal tracciato Json
    Dim parser As JSONParser
    Dim Map1 As Map
    Dim rsuDelGiorno As String
    
    parser.Initialize(calendarioSettimanale)
    Map1 = parser.NextObject
    
    rsuDelGiorno = Map1.Get(giornoDellaSettimana)
    
    Return rsuDelGiorno
    
End Sub
 
Upvote 0
Top