app service does not check for new data when in sleep

dualznz

Member
Licensed User
Longtime User
hi all i have a program that receives jobs from a windows application written in c# that talks to a php page which the devices then listen for using their unique phone ID

the system works without a hitch, the only problem that i have ran into that if the phone is in a deep sleep the service will no longer listen for any new information when the time reaches its limits.

the timer is set to check once an hour if i set it to likes of 10 minutes it works with no problem.

is timer a choice i should be using or is there something else.

here is my service code that runs the checking.

B4X:
Sub Service_Start (StartingIntent As Intent)
   Dim noti As Notification ' notification window
   noti.Initialize
   noti.Icon = "icon"
   noti.Sound = False
   noti.Vibrate = False
   noti.Light = False
   noti.SetInfo("ZWONGA Daily Planner", "Running", "homewindow")
   'noti.SetInfo("ZWONGA Daily Planner", "Has Started", "")
   Service.StartForeground(1, noti)
   ToastMessageShow("ZWONGA Daily Planner has started", False)
   
   If rv.HandleWidgetEvents(StartingIntent) Then Return
   
   httpD.Initialize("httpD")
   

   If SQLmain.IsInitialized = False Then
      SQLmain.Initialize(File.DirInternal, "zwsql4.sql", False)
   End If
   
   'start timer
   timerrefresh.Initialize("timerrefresh", 3600000) ' timer will have a 1 hour wait time befor updating automaticly
   'timerrefresh.Initialize("timerrefresh", 20000)
   timerrefresh.Enabled = True
   
   timercounter.Initialize("timercounter", 300000) ' timer will have a 5 minute wait time befor upding the widget counter from force update
   'timercounter.Initialize("timercounter", 20000)
   timercounter.Enabled = True
   
   mp.Initialize()
   widgetDefaults
End Sub

Sub timerrefresh_Tick
   DateTime.DateFormat = "dd/MM/yy"
   Dim dt As String = DateTime.Date(DateTime.Now)
   
   Dim mydeviceid As String = deviceID
   Dim req As HttpRequest
   req.InitializeGet("http://****************/phploader.php?run=getjoblist&deviceid=" & mydeviceid)
   httpD.Execute(req,1)
End Sub

Sub httpD_ResponseSuccess(Response As HttpResponse, TaskId As Int)
   Response.GetAsynchronously("Response", File.OpenOutput(File.DirInternalCache, "datapull.txt", False), True, TaskId)
End Sub

Sub Response_StreamFinish(Success As Boolean, TaskId As Int)
   Try
   Dim countnew As Int = 0
   Dim reader As TextReader
   reader.Initialize(File.OpenInput(File.DirInternalCache, "datapull.txt"))
   
   ' custom date format
   DateTime.DateFormat = "dd/MM/yy"
   Dim dt As String = DateTime.Date(DateTime.Now)
   
   ' External database import and loop through
   
   Dim id As String
   id = reader.ReadLine
   
   If id = "none" Then
      ' this will not pull any data until there is data to populate with
   Else
   
   Dim i As Int :i = 0
   Do While id <> Null
      i = i+1
      
      Dim idfromdb As String
      idfromdb = reader.readline
      
      Dim jobnumber As String
      jobnumber = reader.ReadLine
      
      Dim customername As String
      customername = reader.ReadLine
      
      Dim address As String
      address = reader.ReadLine
      
      Dim phonenumber As String
      phonenumber = reader.ReadLine
      
      Dim jobdescription As String
      jobdescription = reader.ReadLine
      
      Dim category As String
      category = reader.ReadLine
      
      Dim timestart As String
      timestart = reader.ReadLine
      
      Dim timefinish As String
      timefinish = reader.ReadLine
      
      Dim jobdate As String
      jobdate = reader.readline
      
      Dim statusfk As String
      statusfk = reader.ReadLine
      
      id = reader.ReadLine
      
      ' insert data into the internal database
      
      ' check to see if the information is already in the database table
      results = SQLmain.ExecQuery("SELECT id, jobstatus FROM jobs WHERE ext_id='" & idfromdb & "' AND job_date='" & dt & "'")
      If results.rowcount > 0 Then
         For i = 0 To results.rowcount - 1
            results.Position = i
            Dim perjobid As Int
            perjobid = results.GetInt("id")
            
            Dim perjobstatus As String
            perjobstatus = results.GetString("jobstatus")
            
            ' if the job status has changed, redownload the data
            If perjobstatus == statusfk Then
            Else
               SQLmain.ExecNonQuery("UPDATE jobs SET jobnumber='" & jobnumber & "', customer_name='" & customername & "', address='" & address & "', phone='" & phonenumber & "', description='" & jobdescription & "', category='" & category & "', time_start='" & timestart & "', time_finish='" & timefinish & "', job_date='" & jobdate & "', jobstatus='" & statusfk & "' WHERE id='" & perjobid & "'")
            End If
         Next
      Else If results.rowcount = 0 Then
         ' insert into the database table
         SQLmain.ExecNonQuery("INSERT INTO jobs (ext_id, jobnumber, customer_name, address, phone, description, category, time_start, time_finish, job_date, jobstatus) VALUES ('" & idfromdb & "', '" & jobnumber & "', '" & customername & "', '" & address & "', '" & phonenumber & "', '" & jobdescription & "', '" & category & "', '" & timestart & "', '" & timefinish & "', '" & jobdate & "', '" & statusfk & "')")
         countnew = countnew + 1
      End If
      results.Close
   Loop
   File.Delete(File.DirInternalCache, "datapull.txt")
   reader.Close

   ' check the database to see if there is any empty fields then delete them
   Dim rowid2 As String
      results = SQLmain.ExecQuery("SELECT id FROM jobs WHERE customer_name=''")
      If results.rowcount > 0 Then
         For i = 0 To results.rowcount - 1
         results.Position = i
         rowid2 = results.GetString("id")
         SQLmain.ExecNonQuery("DELETE FROM jobs WHERE id='" & rowid2 & "'")
         Next
      End If
      If countnew > 0 Then
      PlaySound
      rv.SetText("lblNewJobs", countnew & " new jobs added")
      slickcounter = countnew
   Else If countnew = 0 Then
      rv.SetText("lblNewJobs", "0 new jobs added")
   End If
   End If
   counttotaljobs
   rv.UpdateWidget
   Catch
    Log(LastException)
   End Try
End Sub

So this works when the phone is not in a deep sleep with no problems at all.
Any help would be grateful as i am stumped at what to do next.

Cheers
 

dualznz

Member
Licensed User
Longtime User
no i checked my code.

So if i set StartServiceAt() to run at 1 hour checks and the phone is in sleep i can start the following

B4X:
Sub RunUpdateCheck
DateTime.DateFormat = "dd/MM/yy"
   Dim dt As String = DateTime.Date(DateTime.Now)
   
   Dim mydeviceid As String = deviceID
   Dim req As HttpRequest
   req.InitializeGet("http://************/phploader.php?run=getjoblist&deviceid=" & mydeviceid)
   httpD.Execute(req,1)
End Sub

thus the service is running and the phone is in sleep, it will still check every 1 hour intervals ???

or though is it required to use DateTime.Now in the StartServiceAt ?
 
Upvote 0
Top