注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

HttpUtils2下载大文件(四)  

2014-04-18 19:30:32|  分类: B.网络 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
下面是服务模块DownloadService代码

#Region  Service Attributes 
#StartAtBoot: False
#End Region

Sub Process_Globals
Private jobs As Map
Private timer1 As Timer
Type DownloadData (url As String, Target As Object, EventName As String)
Type JobTag (Data As DownloadData,  _
CountingStream As CountingOutputStream, Total As Long)
Private pw As PhoneWakeState
End Sub
Sub Service_Create
jobs.Initialize
timer1.Initialize("timer1", 1000)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Private Sub StartTimer (Target As Object)
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.Vibrate = False
n.Sound = False
n.Light = False
n.SetInfo("Downloading file...", "", Target)
Service.StartForeground(1, n)
timer1.Enabled = True
pw.PartialLock
End Sub

Private Sub EndTimer
Service.StopForeground(1)
timer1.Enabled = False
pw.ReleasePartialLock
End Sub

Public Sub StartDownload(data As DownloadData)
If jobs.ContainsKey(data.url) Then
Log("Ignoring duplicate request.")
Return
End If
Dim J As HttpJob
J.Initialize(data.url, Me)
Dim tag As JobTag
tag.Initialize
tag.data = data
J.tag = tag
jobs.Put(data.url, J)
J.Download(data.url)
If timer1.Enabled = False Then StartTimer(data.Target)
End Sub

Public Sub CancelDownload(url As String)
If jobs.ContainsKey(url) = False Then
Log("Ignoring cancel request.")
Return
End If
Dim job As HttpJob = jobs.Get(url)
Dim jt As JobTag = job.Tag
If jt.CountingStream.IsInitialized Then
jt.CountingStream.Close
Else
jt.Data.url = ""
End If
End Sub

Sub timer1_tick
For Each job As HttpJob In jobs.Values
Dim jt As JobTag = job.Tag
If jt.CountingStream.IsInitialized Then
CallSub3(jt.Data.Target, jt.Data.EventName & "_Progress", _
jt.CountingStream.Count, jt.Total)
End If
Next
End Sub

Sub JobDone(job As HttpJob)
jobs.Remove(job.JobName)
Dim jt As JobTag = job.Tag
If jobs.Size = 0 Then EndTimer
If job.Success Then
CallSubDelayed3(jt.Data.Target, jt.Data.EventName & "_Progress", _
jt.CountingStream.Count, jt.Total)
CallSubDelayed2(jt.Data.Target, jt.Data.EventName & "_Complete", _
job)
Else
Log(job.ErrorMessage)
CallSubDelayed2(jt.Data.Target, jt.Data.EventName & "_Complete", _
job)
End If
End Sub

  评论这张
 
阅读(297)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017