Sub Process_Globals
Private rv As RemoteViews
Private LastUpdate As Long
Private commons As clCommons
End Sub
'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
LogColor("dgmday: receiver_receive: "&FirstTime, 0xFFFF0000)
Log("Intent: "&StartingIntent)
Log("Action: "&StartingIntent.Action)
Log("Extra: "&StartingIntent.ExtrasToString)
If FirstTime Then
rv = ConfigureHomeWidget("lytWidgetD", "rv", 30, "DG Meteo Day",True) 'run the home widget every 30 minutes
commons.Initialize
StartHttpUtils2Service
End If
rv.HandleWidgetEvents(StartingIntent)
If StartingIntent.HasExtra("android.intent.extra.ALARM_TARGET_TIME") Then
'(Intent) Intent { flg=0x14 cmp=eu.dgc.dgmeteo/.dgmd (has extras) }
'Bundle[{android.intent.extra.ALARM_TARGET_TIME=1733507003773, android.intent.extra.ALARM_COUNT=1}]
LogColor("dgmday: called by StartReceiverAt", 0xFF0000FF)
rv_RequestUpdate
End If
End Sub
' It needs the conditional symbol H2U_PUBLIC in Project-Build configuration
Private Sub StartHttpUtils2Service
#if release
Log("dgDay: forces WLAN activation")
If HttpUtils2Service.TempFolder = "" Then
Dim jo As JavaObject
jo.InitializeNewInstance(Application.PackageName & ".httputils2service", Null)
jo.RunMethod("onReceive", Array(Null, Null))
End If
If HttpUtils2Service.TempFolder <> "" Then Log ("okhttp good to go")
#end if
End Sub
'Classic test for Internet availability.
Public Sub TestInternet As ResumableSub
Dim j As HttpJob
j.Initialize("", Me) 'name is empty as it is no longer needed
j.Download("http://www.google.com/")
j.GetRequest.Timeout = 10000
Wait For (j) JobDone(j As HttpJob)
commons.InternetOK = j.Success
j.Release
Return commons.InternetOK
End Sub
'Handles the widget items update or schedules the receiver to run again in 1 minute (if Internet is not available)
Private Sub rv_RequestUpdate
LogColor("dgmDay: rv.RequestUpdate", 0xFFFF00FF)
If Not(commons.InternetOK) Then
Dim res1 As Boolean = False
Log("no internet")
'eventually inform the user that Internet is not available at this time
'rv.SetText("lblTown", "Server unavailable") 'lblTown is a label I have on my home widget UI interface
'rv.UpdateWidget
Log(" Start Internet availability test")
wait for (TestInternet) Complete(res1 As Boolean)
Log(res1)
commons.InternetOK = res1
End If
If Not(commons.InternetOK) Then 'Internet access is still not available. We try again in 1 minute
Log("receiver start scheduled")
StartReceiverAt(Me, DateTime.Now + (1 * DateTime.TicksPerMinute), False) 'run this receiver again in 1 minute
Else 'Internet available. We can call the API service for updated data
Log("dgmDay: data update request")
LastUpdate = DateTime.Now
'here we call what we need in order to receive updated data from the API
wait for (ReadCurrentData) Complete(res11 As Boolean)
'if success, update data visible to the user
'......
'rv.UpdateWidget
Log("dgmDay: data update done")
End If
End Sub