Android Question Using StartServiceAt

Sergey_New

Well-Known Member
Licensed User
Longtime User
I created an example using StartServiceAt. For simplicity, notification permission is set in the device settings. The notification should be generated at the set time.
Two questions:
1. How can I improve the accuracy of the notification time?
2. Why does a notification appear after rebooting the device or emulator, even though the set time has already passed.
Main:
Sub Process_Globals
    Private xui As XUI
     Public Start_Time As Long=10.00
End Sub

Sub Globals
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    Dim nt As Long=setAlarm
    StartServiceAt(EveryDay, nt, False)
End Sub

Sub setAlarm As Long
    Dim t As Long=SetHours(Start_Time)
    Log(t)
    If t > DateTime.Now Then
        Return t
    End If
    Return DateTime.Add(t, 0, 0, 1)
End Sub

Sub SetHours(st As Double) As Long
    Dim hours As Int = Floor(st)
    Dim minutes As Int = Round(60 * (st - hours))
    Return DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), _
  DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), hours, minutes, 0)
End Sub
Service Module EveryDay:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Dim nb As NB6
    Public event_count As Int
End Sub

Sub Service_Create
    Service.AutomaticForegroundmode = Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED
End Sub

Sub Service_Start (StartingIntent As Intent)
    Dim ev As Int=1
    event_count=ev
    If ev<>0 Then
        Dim s As String=("Start Time" & " ") & Main.Start_Time & ":00"
        nb.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(LoadBitmapResize(File.DirAssets, "icon.png", 24dip, 24dip, False))
        nb.ShowWhen(DateTime.Now)
        nb.Color(Colors.Blue)
        nb.Build(s, "", "tag1", Main).Notify(1)
    End If
    Service.StopAutomaticForeground
End Sub

Sub Service_Destroy
    
End Sub

Sub Service_TaskRemoved
    
End Sub
 

Attachments

  • TestNot.zip
    7 KB · Views: 7
Cookies are required to use this site. You must accept them to continue using the site. Learn more…