Android Question How to Schedule Files Download Using a Service And DropBox Sync Library

Mahares

Expert
Licensed User
Longtime User
My Knowledge of ‘Service’ module is virtually non-existent, but I read just about every post that has the word 'service'. The app starts, links to a DropBox account, which it does and then download 6 files ranging from 1 KB to 3 MB from DropBox server, which it does. Here are my problems:
1. It only downloads twice: the first time after compile and the next time.. It does not repeat although I schedule every 3 minutes (later I plan to change it a bigger time interval such as 1 day). When the 3rd and 4th shedules run, the log says: ‘Service (name of service) Start’, but no download happens.
2. I want the app to work in the background. I only want to show the toast messages.
If you have a good grasp of service I need your help


B4X:
'In Activity module. Full code.
Sub Activity_Create(FirstTime As Boolean)
  StartService(svcDownload)
  Activity.Finish
End Sub

'In Service module
#StartAtBoot: false

Sub Process_Globals
  Dim Manager As DbxAccountManager
  Dim keyDBX As String = "xxxxrspn1tlyyy”
  Dim secretDBX As String = "wwww08ckvvvvv"
End Sub


Service_Create
  Manager.Initialize(keyDBX, secretDBX, "Manager")
  Manager.LinkAccount  'must link to account
  Manager.AutoSync =True  '
  Mytimer.Initialize("MyTimer",1000)  'timer allows accnt to link before download
   t1=DateTime.Now
   Mytimer.Enabled=True
End sub

Sub Service_Start (StartingIntent AsIntent)  'repeat download every 3 minutes
  Dim p As Period
  p.Minutes = 3
  Dim NextSchedule As Long = DateUtils.AddPeriod(DateTime.Now, p)
  StartServiceAt(svcDownload, NextSchedule,True)
End Sub

Sub MyTimer_Tick
  Dim t2 AsLong
  t2=DateTime.Now
  If t2-t1 >=5000  Then  'wait 5 sec
   Mytimer.enabled=False
    CallSubDelayed("","Download")  'this is the download routine sub
  End If
End Sub
'Download sub is in service module svcDownload
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
1. It only downloads twice: the first time after compile and the next time.. It does not repeat although I schedule every 3 minutes (later I plan to change it a bigger time interval such as 1 day). When the 3rd and 4th shedules run, the log says: ‘Service (name of service) Start’, but no download happens.
Its difficult to say since you are using a timer. Can you do without the timer?
Why not call 'Download' after the manager login (Sub Manager_AccountReady (Success AsBoolean)) event?

2. I want the app to work in the background. I only want to show the toast messages.
If you have a good grasp of service I need your help
Should be fine the way you are doing it. Just check on Start Service at boot if you want it to start on device reboot.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
As suggested by TDS ( thank you TDS), I did away with the timer , put the download sub at the end of the Manager_Account ready sub and changed the #StartatBoot from False to True. None of the above changes made a difference. It downloads at the end the compile. I close the green compile button, it runs a second time and that is it. The notification icon is displayed, but no luck.

B4X:
#StartAtBoot: True

Sub Manager_AccountReady (Success As Boolean)
   MyListtwo.Initialize   'initialize list to start filling new list
   Log("Account Ready: " & Success)
   If Success Then
     'put the files in a list code here
   End If   
   CallSub(Me,"Download")  'start downloadt download
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@TDS: You are correct. There was no reference to the download sub in my Service_Start code, which was the cause of the problem. I added a reference to it and the download seems to work on the prescribed time interval. I am going to extend the scheduled time interval to few hours and test again. I will report back to you if any issues pop up.
Thank you very much. You are an asset to this forum.
 
Upvote 0
Top