#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
Dim HC As HttpClient
'Activity is expected to set URL
Dim URL As String
Dim Target As OutputStream
Dim JobStatus As Int
Dim STATUS_NONE, STATUS_WORKING, STATUS_DONE As Int
STATUS_NONE = 0
STATUS_WORKING = 1
STATUS_DONE = 2
Dim DoneSuccessfully As Boolean
Dim Notification1 As Notification
End Sub
Sub Service_Create
HC.Initialize("HC")
'Notification1.Initialize
'Notification1.Icon = "icon" 'use the application icon file for the notification
'Notification1.Vibrate = False
If File.Exists(File.DirInternal, "database.sqlite") Then
File.Delete(File.DirInternal, "database.sqlite")
End If
URL = "http://myurl/database.sqlite"
Target = File.OpenOutput(File.DirInternal, "database.sqlite", False)
End Sub
Sub Service_Start(StartingIntent As Intent)
'URL and Target should be set by the calling module
Dim request As HttpRequest
request.InitializeGet(URL)
HC.Execute(request, 1)
JobStatus = STATUS_WORKING
'Notification1.SetInfo("Download Service example", "Downloading: " & URL, Main)
'Notification1.Sound = False
'Make sure that the process is not killed during the download
'This is important if the download is expected to be long.
'This will also show the status bar notification
' Service.StartForeground(1, Null)
End Sub
Sub HC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
'ToastMessageShow("Error downloading file: " & Reason, True)
DoneSuccessfully = False
Finish
End Sub
Sub HC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
'Asynchronously download the stream
Response.GetAsynchronously("Response", Target, True, TaskId)
End Sub
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
If Success = False Then
'ToastMessageShow("Error downloading file: " & LastException.Message, True)
Else
'ToastMessageShow("Download successfully.", True)
End If
DoneSuccessfully = Success
Finish
End Sub
Sub Finish
Log("Service finished downloading")
JobStatus = STATUS_DONE
'Notify the activity that the download has finished.
'It will do nothing if the activity is currently paused.
CallSub(Main, "FinishDownload")
' Service.StopForeground(1) 'Return the service to the "background" (also removes the ongoing notification)
If IsPaused(Main) Then
'The activity is paused. The user is probably busy with some other activity.
'Notify the user that the download has finished
'Notification1.Sound = True
'Notification1.SetInfo("Download Service", "Download complete", Main)
'Notification1.AutoCancel = True
'Notification1.Notify(1)
End If
End Sub
Sub Service_Destroy
End Sub