Android Question "StartServiceAt" and "StartServiceAtExact" Delay

bixmatech

Member
Licensed User
Hi...

I used "StartServiceAt" and "StartServiceAtExact" for starting Alarm Service on a specific time. but the problem, some times the alarm start at the exact time , and other times delay from 1 to 5 minuts and sometimes before seconds from the selected time.

This a part from the code I used :

B4X:
Sub StartAlarm
    Dim hours As Int = 1    ' just for testing
    Dim mins As Int  = 15   ' just for testing
   
    Dim alarm As Long =  DateTime.DateParse(DateTime.Date(DateTime.Now) + (hours * DateTime.TicksPerHour + mins * DateTime.TicksPerMinute))
    If alarm < DateTime.Now Then     alarm = DateTime.Add(alarm, 0, 0, 1)
           
    Dim h As Int = Floor((alarm - DateTime.Now) / DateTime.TicksPerHour)
    Dim m As Int = ((alarm - DateTime.Now) - h * DateTime.TicksPerHour) / DateTime.TicksPerMinute

    StartServiceAt("AlarmService", (DateTime.Now+((h*60*60*1000)+(m*60*1000))), True)
    'StartServiceAtExact("AlarmService", (DateTime.Now+((h*60*60*1000)+(m*60*1000))), True)
End Sub


B4A : 6.8

Thank you ....
 

bixmatech

Member
Licensed User
My results after testing ...

I used this code :

B4X:
Sub SetExactAndAllowWhileIdle (Time As Long, ServiceName As String)
    Dim p As Phone
    If p.SdkVersion < 23 Then
        StartServiceAtExact(ServiceName, Time, True)
    Else
        Dim in As Intent
        in.Initialize("", "")
        in.SetComponent(Application.PackageName & "/." &  ServiceName.ToLowerCase)
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim am As JavaObject = ctxt.RunMethod("getSystemService", Array("alarm"))
        Dim pi As JavaObject
        pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getService", _
       Array(ctxt, 1, in, 134217728))
        am.RunMethod("setExactAndAllowWhileIdle", Array(0, Time, pi))
    End If
End Sub

I tested for 5 times on "Samsung Note 4" 6.0.1
1-3 : perfect , started on the specific time.
4 : not work ( I think the device was in deep sleep )
5 : before nearly 20 seconds from the selected time.

is that mean , there is no solution, to start the alarm on the specific time on Android 6?

Thank you
 
Upvote 0

bixmatech

Member
Licensed User
Have you tested it in Release mode?
Are you sure that you haven't killed the app with a swipe?


"killed the app with a swipe?" like what?

- Test it in release mode

- I close the app by using Activity.Finish

- For service : ( is this code will effect ? )
Sub Service_Destroy
StopService("")
CancelScheduledService("")
End Sub

- the service will start after a random time but everyone more than an hour.

- Today i tested again but its delayed for about 5 minutes. :(
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

bixmatech

Member
Licensed User
- Thank you for the explanation... I didn't use swipe .

- When the "Service" start , will calculate another time ,then restart it self one again .. so in this case it's better remove "StopService" and "CancelScheduledService" ?

- I'll try to whitelist my app and test it for today

Thank you so much ...
 
Upvote 0

bixmatech

Member
Licensed User
Never call CancelScheduleService unless you want to cancel the alarm. Calling StartServiceAt (or one of the similar methods) will cancel the previous one anyway.

I tested 4 times after building.
1-3 : Started on the specific time. ( same as before, first 3 times after building ,started on the time )
4 : Two minutes delay. :(

Note :
removed "StopService" and "CancelScheduledService"
whitelist my app
test on Note 4 , 6.0.1

any idea ??

thank you so much ...
 
Upvote 0

bixmatech

Member
Licensed User
I'm afraid that there is no other method to schedule a service. Try it with targetSdkVersion set to 14. It might improve the behavior.

Thank you ...

Actually, Now its "14" :(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>

So there's no solution for Android 6 and above to start the service on the specific time ? :(

Thanks.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
So how does the alarm clock do it? It's just an app, too.
If you study how the alarm clock works, you will see that the notification about "there will be an alarm shortly" appears about 1 hour before the real alarm time is reached. Are they using a running service from that time to be sure about the accuracy ?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
there will be an alarm shortly

Never seen this on my phone (and sad but true - I often wake up 20 mins. before I have to get up :) )

Another one: If I "switch off" my phone (=deep coma or whatsoever), the alarm still works (phone is started) which is accurate, too. Are services still running in that mode or is it a hw thing?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Never seen this on my phone (and sad but true - I often wake up 20 mins. before I have to get up :)
Really ? I always see it on mine :) reason why I thought it could be that
Screenshot_2017-06-07-15-08-08-820.jpeg
 
Upvote 0
Top