Android Question Schedule a Service to Upload a Text File Created From a SQLite Table

Mahares

Expert
Licensed User
Longtime User
The app is designed to link to a DropBox account using DropBox Sync Lib. It creates a text file from a SQLite table and uploads the text file to DropBox. The service allows it to repeat creating the text file and the upload every 3 minutes (eventually every few hours).

9:00 AM FIRST RUN: Compile run. Authenticate account, create text file and upload OK.
9:03 AM SECOND RUN: Create file and upload to Dropbox OK. But error shows: Program paused on line 64 which is the End Sub line of the Service_Start sub
9:06: AM THIRD RUN: Text file is created, but no more upload to Dropbox.

Can someone help me sort this out so it works on a scheduled basis and no ghost activity is displayed when the upload is finished.

Thank you

B4X:
'BELOW ACTIVITY MODULE
Sub Process_Globals
  Dim Manager As DbxAccountManager
  Dim keyDBX As String = "bjeokwhf0fy5xudl"
  Dim secretDBX As String = "p094farez7t00"
  Dim SQL1 As SQL
  Dim Cursor1 As Cursor
End Sub
 
Sub Globals
  'pertinent variables declared here.
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    Manager.Initialize(keyDBX, secretDBX, "Manager")
    Manager.LinkAccount
  End If
  'Here initialize database code
  CreateDailyTextFile  'sub to create text file from a SQLite table. WORKS OK
End Sub
 
Sub Activity_Resume
End Sub
 
Sub Activity_Pause (UserClosed AsBoolean)
End Sub
 
Sub CreateDailyTextFile
  'Here I have code that creates the text file from a SQLite table
End Sub
 
Sub Manager_AccountReady (Success As Boolean)
  Log("Account Ready: " & Success)
  If Success=True Then
    MySuccess=Success
    Log("Success: " & MySuccess)
    UpLoadFile
    Activity.Finish
    StartService(svcDownload)
  End If
End Sub
 
Sub UpLoadFile
  Manager.UpLoadFile(DBFilePath,MyDailyTextFile,"/Import",MyDailyTextFile)
  Log("Text file  " & MyDailyTextFile & " was uploaded successfully")
End Sub


B4X:
'BELOW IS FULL SERVICE MODULE CODE:
#Region  Service Attributes
  #StartAtBoot: True
#End Region
Sub Process_Globals
  Dim sNotify As Notification
End Sub
 
Sub Service_Create
  sNotify.Initialize
  sNotify.Icon = "icon"
  sNotify.SetInfo("HHE","Auto Export Daily Files to DropBox",Main)
  sNotify.Sound = True
  sNotify.Notify(1)
  Service.StartForeground(1,sNotify)
End Sub
 
Sub Service_Start (StartingIntent AsIntent)
  Dim p As Period
  p.Minutes = 3  'minutes . In future, I will change to hours
  Dim NextSchedule As Long = DateUtils.AddPeriod(DateTime.Now, p)
  StartServiceAt("",NextSchedule,True)
  StartActivity(Main)
End Sub     'PROGRAM PAUSED ERROR HERE
 

Mahares

Expert
Licensed User
Longtime User
1. Indeed, The manager.linkAccount is done from the activity module as I show it in my code above, not from the service. Please take another look at my posted code. Something is not right. I do not have anything in activity pause or resume.
2. The error message pops up only in the 2nd run after I disconnect the debugger and the 2nd schedule is run. The program does not stop. All I see on the screen is: 'Program paused on line 64 which is the End Sub line of the Service_Start sub.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I did test it in release mode. It is still no good. Here is the sequence of the schedules:
1st run: after compile: Create and upload : Good, but activity does not vanish.
2nd run: 3 minutes later, Create text file, but no upload to follow. Ghost activity does not vanish.
3rd and subsequent schedules: Create text file, but no upload of file to DropBox. Ghost activity does not vanish.
It never raises the Manager_AccountReady event in the 2nd and subsequent runs, which is essential in uploading the file.
Could someone please take a look at my activity and service modules code and see if the placement of startservice and activity.finish are where they are supposed to be and why it stops uploading after the first run..
Thank you
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe you can export project as zip and upload it here... I´m sure there is someone who will have a look at it ;-)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@DonManfred: If you are familiar with DropBox, in order to upload to DropBox, there is a key value and a secret value that are part of the project that you must have to be able to upload to DropBox. Also, you must have a user and a password to authenticate the account. I can, however, consolidate it and export it as zip without the database and the secret numbers, but I am not sure you can test it. However, you will be able to see the full code. Would that help you guys.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Actually almost the entire code is posted in my post#1 (Activity and Service), except for the sub that creates the text file from SQLite table and it has nothing to do with the program not running well. I will try to create a smaller project that will have everything except that I will replace the database with the text file to be uploaded.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Attached is the complete functional project that illustrates the problems I explained in the upper posts. To simplify, I replaced the text file that is created from the database with the text file itself and it is included in the project.
The problems:
1. The scheduled upload of the text file to the DropBox folder only works the first run. After that, the service runs, but no upload.
2. There is always a ghost activity at the end. The activity does not vanish as it is supposed to.
Thanks in advance for helping.
 

Attachments

  • DropBox_Upload_TexFile_Scheduled_Service.zip
    9 KB · Views: 179
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@DonManfred: Thank you very much for taking a look. By virtue of you moving some of the code around, it is working well now. I will apply the changes to my real application with the file creation. I am confident it will be fine also when I test it. The only little glitch, which is insignificant is when you first authenticate the DropBox account, it asks for the authentication 3 consecutive times as opposed to one before it approves it. This is not a big deal.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@DonManfred: By virtue of you moving some of the code around, it is working well now. I will apply the changes to my real application with the file creation. I am confident it will be fine also when I test it.

Tell us about whether it work with your original now or not
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
When I run the app at X minutes intervals, it works fine, but when I tried to run it at a given time of the day, it only runs the first time at compile and then stops running. I replaced the code in the:
Sub Manager_AccountReady (Success As Boolean)

Replaced this:
B4X:
Dim p AsPeriod
  p.Minutes = 3  'minutes
  Dim NextSchedule As Long = DateUtils.AddPeriod(DateTime.Now, p)
  StartServiceAt(svcDownload,NextSchedule,True)
    Activity.Finish

With this:
B4X:
StartServiceAt(Me,  NextTimeInstance(8, 40) ,True)  'Example: run it once a day: 8:40 am
Activity.Finish
B4X:
Sub NextTimeInstance (Hours As Int, Minutes As Int) As Long
  Dim today As Long = DateTime.Now
  today = DateUtils.SetDateAndTime(DateTime.GetYear(today), DateTime.GetMonth(today), _
  DateTime.GetDayOfMonth(today), Hours, Minutes, 0)
  If today < DateTime.Now Then
    Dim p As Period
    p.days = 1
    Dim tomorrow As Long = DateUtils.AddPeriod(today, p)
    Return tomorrow
  Else
    Return today
  End If
End Sub

EDIT: Changed: StartServiceAt(Me, NextTimeInstance(8, 40) ,True)
to: StartServiceAt(svcDownload, NextTimeInstance(8, 40) ,True)
and now it works well. merci
 
Last edited:
Upvote 0
Top