Android Question intent not fire if screen is locked

Hannibal

Member
I'm using intent/receiver to do some stuff at specified time.
Since I would like it work like an alarm clock that still work if screen is locked, I base my code to that https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/
on old android like pie and oreo it work fine. On newest if screen is locked it do nothing until I unlock it, once unlocked it fire. I degugged with bridge, but I not found any exception..
So after reading on the forum I disabled battery optimization for my app, but I get the same behavior. I have also an intent-receiver to do stuff at boot, and it work fine everytime an all phones I tested.
Am I missing something?
 

Hannibal

Member
Are you using StartReceiverAt ? Are you showing a notification from the receiver? How do you see that it is not working?
yes, I'm using it. I'm showing a notification. I setted the intent for 9 AM using a function that you posted in another tutorial, at this time I did not get the notification, so I waited until 9.15 and then I unlocked the phone, when I unlocked I get the notification after some seconds.
I done this test for some days and I found that if I not unlock the phone but I wait some hours (like unlocking it at 12.30), it had showed the notification at 10AM ( 1 hour later the time setted).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 1
Solution

Hannibal

Member
B4X:
Sub FindNextTime(Times As List) As Long
    Times.Sort(True)
    For Each st As Double In Times
        If SetHours(st) > DateTime.Now Then
            Return SetHours(st)
        End If
    Next
    Return DateTime.Add(SetHours(Times.Get(0)), 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
I can't find the tutorial link but this is the code is that.

I'll give a try.
 
Upvote 0
Top