Hi Everyone,
I'm trying to get my head around scheduling services to wake up then make it sleep. Can you look at my code because this simple app gets stuck in a loop waking up over and over again as soon as it is woken up using SetExactAndAllowWhileIdle. This indicates I'm missing some coding to put it to sleep or I'm not using the correct coding.
The logic for this simple app is the user will click on a button to start the service. Once the service is started it uses kvs.get to retrieve the time when it should start up again then it should go back to sleep and wake up at a later time. It does actually wake back up but then it gets stuck in a loop.
This is the main module:
This is the service:
I'm trying to get my head around scheduling services to wake up then make it sleep. Can you look at my code because this simple app gets stuck in a loop waking up over and over again as soon as it is woken up using SetExactAndAllowWhileIdle. This indicates I'm missing some coding to put it to sleep or I'm not using the correct coding.
The logic for this simple app is the user will click on a button to start the service. Once the service is started it uses kvs.get to retrieve the time when it should start up again then it should go back to sleep and wake up at a later time. It does actually wake back up but then it gets stuck in a loop.
This is the main module:
B4X:
#Region Project Attributes
#ApplicationLabel: Service Wakeup Test
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private kvs As KeyValueStore
Private ButtonStartService As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
' Initialize.
'------------
kvs.Initialize(File.DirInternal, "Settings")
kvs.Put("ServiceWakeupTime", "18:00")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
' Event handlers.
'----------------
Sub ButtonStartService_Click
StartService(ServiceWakeup)
ToastMessageShow("The time to wake up at " & kvs.Get("ServiceWakeupTime") & _
" is now set." , True)
StopService(ServiceWakeup)
Activity.Finish
End Sub
This is the service:
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private sf As StringFunctions
Private kvs As KeyValueStore
Private nNotify As Notification
Private phWakeState As PhoneWakeState
Private lngTimeToWakeup As Long
End Sub
Sub Service_Create
phWakeState.PartialLock
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
sf.Initialize
nNotify.Initialize
nNotify.Icon = "icon"
nNotify.Sound = False
nNotify.Light = False
nNotify.Vibrate = False
nNotify.SetInfo("Service Wakeup Test", "I'm at Service_Create.", "")
nNotify.Notify(1)
kvs.Initialize(File.DirInternal, "Settings")
lngTimeToWakeup = TimesToTicks(sf.Mid(kvs.Get("ServiceWakeupTime"), 1, 2), _
sf.Mid(kvs.Get("ServiceWakeupTime"), 4, 2), "00")
SetExactAndAllowWhileIdle(lngTimeToWakeup, "ServiceWakeup")
End Sub
Sub Service_Start (StartingIntent As Intent)
nNotify.Initialize
nNotify.Icon = "icon"
nNotify.Sound = False
nNotify.Light = False
nNotify.Vibrate = False
nNotify.SetInfo("Service Wakeup Test", "I'm at Service_Start.", "")
nNotify.Notify(2)
Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
phWakeState.ReleasePartialLock
StopService(Me)
End Sub
Sub Service_Destroy
End Sub
' Misc sub routines.
'-------------------
Sub SetExactAndAllowWhileIdle(Time As Long, ServiceName As String)
Dim Phone As Phone
If Phone.SdkVersion < 23 Then
StartServiceAtExact(ServiceName, Time, True)
Else
Dim in As Intent
in.Initialize("", "")
in.SetComponent(Application.PackageName & "/." & ServiceName.ToLowerCase)
Dim Jin As JavaObject = in
Jin.RunMethod("setAction", Array(Null))
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
Sub TimesToTicks (Hours As String, Minutes As String, Seconds As String) As Long
Dim strTime As String
DateTime.TimeFormat="HH:mm:ss"
' Pad an extra 0 if needed for the time strings.
'-----------------------------------------------
If Hours.Length=1 Then
Hours="0" & Hours
End If
If Minutes.Length=1 Then
Minutes="0" & Minutes
End If
If Seconds.Length=1 Then
Seconds="0" & Seconds
End If
' Build time string.
'-------------------
strTime = Hours & ":" & Minutes & ":" & Seconds
' Return the ticks value of the time string.
'-------------------------------------------
Return DateTime.TimeParse(strTime)
End Sub
Last edited: