Net: POP3 in a service module

jdonth

Member
Licensed User
Longtime User
Is there any reason why the Net:pOP3 code will not run in a service module rather than in an Activity? I am trying to implement the code and List_Completed is not being fired nor am I getting any error messages (that I can find).

Thanks,
Joe
 

jdonth

Member
Licensed User
Longtime User
There is no problem. Did you check the logs (including the unfiltered logs)?
If you do not find the error then please post the service code.

Here is the service code. I hard-coded my POP3 server parameters (which I have removed from this post) so I'm sure they are correct.

My pop3 server requires authentication - might that be a problem?

Any help would be appreciated.

B4X:
' popService - Service module
Sub Process_Globals
 Dim POP As POP3
 Dim Notify As Notification
 Dim manager As PreferenceManager
End Sub

Sub Service_Create
 Notify.Initialize
 Notify.Icon = "icon"
 Notify.Vibrate = False
 Notify.AutoCancel = True
End Sub

Sub Service_Start
 Dim tmp, interval, host, user, pass As String
 Dim port As Int
 Dim ssl As Boolean
 Dim messages As Map

 host = manager.GetString("host")
 tmp = manager.GetString("port")
 If IsNumber(tmp) Then port = tmp Else port = 110
 user = manager.GetString("user")
 pass = manager.GetString("password")
 ssl = manager.GetBoolean("ssl")
 interval = manager.GetString("interval")
 If interval = "" Then interval = 1
 If interval <= 0 Then
  manager.SetString("interval","5")
  interval = manager.GetString("interval")
 End If

 POP.Initialize(host,port,user,pass,POP)
 POP.UseSSL = ssl
 POP.ListMessages
 StartServiceAt("", DateTime.Now + interval * DateTime.TicksPerMinute, False)
 DateTime.TimeFormat = "h:mm:ss a" 
 manager.SetString("NextTime","   Next check: " & DateTime.Time(DateTime.now + interval * DateTime.TicksPerMinute) & " ")
 If IsPaused(Main) = False Then CallSub(Main,"DisplayTitle")
End Sub

Sub pop_ListCompleted (success As Boolean, messages As Map)
 If success Then
  Log("Count:" & messages.Size)
  For i = 0 To messages.Size - 1
   POP.DownloadMessage(messages.GetKeyAt(i), True) 'Download all messages and delete them
  Next
 Else
  Log(LastException.Message)
 End If
 POP.Close 'The connection will be closed after all messages are downloaded
End Sub

Sub pop_DownloadCompleted (Success As Boolean, MessageId As Int, Message As String)
 Log("Download: " & Success & ", " & MessageId)
 If Success Then 
  Log(Message)
  Log(Message.Length)
  Log(MessageId)
 Else 
  Log(LastException.Message)
 End If
End Sub 


Sub Service_Destroy
End Sub
 
Upvote 0

jdonth

Member
Licensed User
Longtime User
A question about Service_Create vs Service_Start.

If the user changes any of his POP3 information, how can I re-init the POP3 connection if the code is in the Create section? Does it hurt to leave it in the Start section?
 
Upvote 0

jdonth

Member
Licensed User
Longtime User
Erel,

Thank you for your response. As a follow-up to your answer, let me ask a design question.

I am developing an app that monitors multiple POP3 email accounts. If you were developing an app like this, would you use one service module and loop through each of the user's email accounts or would you create multiple service modules and have each module monitor one email address?

If you would recommend the "one service module" approach, how would you code the POP.Init / POP.close logic?

If you would recommend the multiple service module approach, do I have to write and include multiple service modules in the app or can I create multiple instances of a single service? If so, how?

Thanks,
Joe
 
Upvote 0

jdonth

Member
Licensed User
Longtime User
Does that mean that I can use the POP.Initialize in the Service_Start routine (rather than the Service_Create) as long as I close the POP connection (POP.Close) before evoking the service again?
 
Upvote 0

jdonth

Member
Licensed User
Longtime User
Thanks Erel. Your B4A compiler and development platform makes android development almost too easy and I am really impressed with how it keeps getting better. Thanks for all your effort with the product and the amazing support!
 
Upvote 0
Top