Hello,
Here my code, why widget not update after the download finish?
Here my code, why widget not update after the download finish?
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 hc As HttpClient
Dim target As OutputStream
Dim rv As RemoteViews
Dim image1 As Bitmap
Dim out As OutputStream
End Sub
Sub Service_Create
' Configure the homescreen widget. We let it update every 2 hours here. This is
' normally not necessary but I want to be sure that the clock restarts if there
' is any problem.
rv = ConfigureHomeWidget("widget", "rv", 120, "image du jour")
hc.Initialize("hc")
End Sub
Sub Service_Start (StartingIntent As Intent)
' If a widget is removed from homescreen we don't need to do anything
If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED" Then Return
' Handle the widget events (RequestUpdate and Disabled)
If rv.HandleWidgetEvents(StartingIntent) = False Then
' If the action is not handled by HandleWidgetEvents() then we
' probably were called by StartService() or StartServiceAt().
' So just update the widget.
rv_RequestUpdate
End If
' We have to be sure that we do not start the service
' again if all widgets are removed from homescreen
If StartingIntent.Action <> "android.appwidget.action.APPWIDGET_DISABLED" Then
StartServiceAt("", TimeToNextMinute, False)
End If
End Sub
Sub rv_RequestUpdate
' Just update the time on the widget
code.CallServer
End Sub
Sub rv_Disabled
' If all widgets are removed from homescreen cancel the next schedule and
' stop the service
CancelScheduledService("")
StopService("")
End Sub
Sub Service_Destroy
End Sub
Sub ImageView1_Click
rv.SetImage( "imageview1",image1)
rv.UpdateWidget
End Sub
Sub TimeToNextMinute As Long
Dim ret As Long
DateTime.TimeFormat = "HH:mm:ss"
ret=DateTime.TimeParse("10:00:00")
Log(DateTime.Time(ret))
ret = (ret + DateTime.TicksPerDay)
Log(DateTime.Time(ret))
Log(DateTime.date(ret))
Return ret
End Sub
Sub HC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log("Success"&TaskId)
Target = File.OpenOutput(File.DirInternalCache, "image.jpg", False)
Response.GetAsynchronously("Response", Target, True, TaskId)
End Sub
Sub HC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("erreur")
Log(reason)
Log("ResponseError"&TaskId)
End Sub
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
If Success = False Then
ToastMessageShow("Error downloading file: " & LastException.Message, True)
Log("StreamFinish erreur"&TaskId)
Else
Log("StreamFinish succes"&TaskId)
Dim image As Bitmap
If File.Exists(File.DirInternalCache, "image.jpg") Then
image=LoadBitmap( File.DirInternalCache, "image.jpg")
Dim canvas1 As Canvas
Dim destrect As Rect
Dim rapport As Float
Dim largeur,hauteur As Int
If image.Height>image.Width Then
rapport=image.Height/410dip
hauteur=410dip
largeur=image.Width/rapport
Else
rapport=image.Width/274dip
largeur=274dip
hauteur=(image.Height/rapport)
End If
Log(largeur)
Log(hauteur)
image1.Initializemutable(largeur,hauteur)
canvas1.Initialize2(image1)
destrect.Initialize(0,0,largeur,hauteur)
canvas1.DrawBitmap( image,Null,destrect)
'
Log(image.Width)
Log(image.Height)
' out=File.OpenOutput( File.DirRootExternal,"image10.png",False)
' Log( File.DirDefaultExternal)
' canvas1.Bitmap.WriteToStream( out,100,"PNG")
' out.close
rv.SetImage( "imageview1",image1)
rv.UpdateWidget
End If
End If
End Sub
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub CallServer
PostUrl = "http://www.b4x.com/android/images/logo2.png"
Dim req As HttpRequest
req.Initializeget( PostUrl)
widget.hc.Execute(req, 0)
End Sub