Android Question [SOLVED] A "better" PhoneWakeState

lemonisdead

Well-Known Member
Licensed User
Longtime User
I experienced some troubles with some phones which gone to sleep ignoring my PhoneWakeState arguments. Especially when the lockscreen is locked.
Isn't it possible to be provided a way to ensure a service won't go to sleep even if the user pressed the ON/OFF button ?
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
I thought it was because of new Android policies as I had less problems with versions under 4.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
I beg your pardon, I was absolutely unclear. I should have better written : "I thought those behaviour could have been generated because of any changes in the Android policies".
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Maybe I do not understand. What I've understood is :
  • PartialLock : should prevent the CPU going to sleep even if the screen is locked by the user
  • KeepAlive : should a prevent the screen to lock but when the user presses the power button as no effect
I have tried with those phones :
  • Samsung Galaxy S4
  • Samsung Galaxy Note II
  • Samsung Galaxy Note2
  • Samsung Galaxy S (GT-I9000)
  • Samsung Galaxy S3
  • Samsung Galaxy Xcover2
  • Samsung Galaxy Ace (GT-S5839i)
  • LG G2
  • Motorola Moto G
  • Motorola Moto X
  • Acer Liquid E2 Duo
  • Sony Ericsson Xperia S
  • HTC Desire HD
  • Wiko Cink Plus
  • Archos 50 Platinum
  • Archos 40 Titanium
  • Polaroid PROG95D

As you kindly spend your time to help fact that I do thank you about very much, below is a sample of code. I created this sample only for the demonstration. Its purpose is to make a beep every hour. I have thought at two services :
  • Planner : in charge of scheduling the DoSomething execution
  • DoSomething : the service which beeps
In DoSomething I change the PhoneWakeState only for demonstration.

Service Planner
B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Private sf As StringFunctions
    Private P As PhoneWakeState
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

    P.PartialLock

    If Not(sf.IsInitialized) Then
        sf.Initialize
    End If

        Private Hour As String
        Hour=sf.Left(DateTime.Time(DateTime.Now),2)
   
        If Not(IsNumber(sf.Right(Hour,1))) Then
            Hour=sf.Left(Hour,1)
        End If
   
        Hour=Hour+1
   
        Dim Target As String
        Target=Hour&":00:00"
'        Log("Target: " & Target)
'        Log(DateTime.Time(DateTime.TimeParse(Target)))
   
        StartServiceAt(DoSomething,DateTime.TimeParse(Target),True)
  
    End If


    StopService("")


End Sub

Sub Service_Destroy

End Sub


Service DoSomething
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private P As PhoneWakeState
    Private T as Timer
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    P.ReleasePartialLock
    P.KeepAlive(False)

 
    If Not(T.IsInitialized) Then
        T.Initialize("T",10*1000)
    End If
    T.Enabled=True


    ToastMessageShow("I do Beep",False)

    Dim b As Beeper
    b.Initialize2(200,1500,b.VOLUME_VOICE_CALL)
    b.Beep

    StartServiceAt(Planner,DateTime.now+2000, True)

   
End Sub

Sub Service_Destroy
    P.ReleaseKeepAlive
    P.PartialLock
End Sub

Sub T_Tick
    T.Enabled=False
    StopService("")
End Sub
 
Last edited:
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hi Erel,
Nope I do not. I thought I could schedule services using StartServiceAt even if the phone is locked.
This not to run a service all day long.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
OK for the two seconds. But if you need to run the service at the hour each hour ?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
That was exactly the reason why I have asked the question about : on some phones the device doesn't wake.

But a bypass should exist or those phones couldn't remind the calendar's events or generate alarms at exact time. Am I wrong about ?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Just to go over your code, it doesnt look right to me.
You set the phone wakestate in the first service, uptil Planner is running the wakestate will prevent the CPU from sleeping.
When Planner stops and DoSomething starts, there is a period of idle time, where even though your wakestate is active, if your app is not doing anything it will be killed and the wakestate will be killed.
Remember what wakestate will do is to prevent the CPU from sleeping when your code is running.
It will not prevent the OS from killing the service if it runs low on resources. Android can kill the service with an active wakestate.

I have tested this consistently on an GalaxyS and S3 and I can confirm that using StartServiceAt will always wake up the device, then the partiallock will keep the CPU active until the code is running, then turning partiallock off, will let it go back to sleep.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Many thanks for your clear explanation. I was effectively wrong by thinking the Planner will change the CPU mode permanently. That's the point.
 
Upvote 0
Top