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.
[CODE lang="b4x" title="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[/CODE]
[CODE lang="b4x" title="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[/CODE]
 

Attachments

It's been two weeks since I wrote this post.
Perhaps I phrased my question poorly.
What should I clarify in my question to make it clearer?
I have one more question.
3. Will I receive notifications if power saving mode is enabled?
 
Upvote 0
1760850705362.png



And if you want it to be exact: https://www.b4x.com/android/forum/threads/start-receiver-at-exact-time.148185/#content
 

Attachments

  • 1760850683489.png
    1760850683489.png
    23.6 KB · Views: 292
Upvote 0
Provide more information. Post the relevant code and describe what happens.

Make sure to test it in release mode.
I finally managed to rework the example for B4A. I've attached a working example. Unfortunately, after installing and uninstalling the app for the first time, the permission request doesn't appear. The permission remains. How can I fix this?
 

Attachments

Upvote 0
Hi Sergey,
In the manifest manager you have <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="29"/> and AddPermission(android.permission.SCHEDULE_EXACT_ALARM).
API level 29 = Android 10. The current B4A version uses API-35. In Android 12, Google introduced a new permission called "SCHEDULE_EXACT_ALARM" for API level 31 (Android 12) or higher and having this permission. Subsequently, with API-31 and AP-32, Google made additional changes to these restrictions. We do not know which Android versions you are targeting. You can read more at https://www.esper.io/blog/android-13-exact-alarm-api-restrictions.
 
Upvote 0
Unfortunately, after installing and uninstalling the app for the first time, the permission request doesn't appear.
I wrote it incorrectly. My example doesn't request permission for the alarm. It works without permission. I'd like the user to be able to choose whether to use the alarm or not. Please add the ability to receive the alarm request.
 
Upvote 0
Back
Top