Android Question pause service when screen turns off?

anOparator

Active Member
Licensed User
Longtime User
What is the way to prevent a Service from firing when the phone is in Sleep state (the screen turns off)?
My B4A wallpaper changer works great. I changed the update time in the Activity module to 20 seconds to see what else was happening and when I manually woke the screen, the screen updated multiple times in 5 seconds:
B4X:
 Sub Activity_Pause (UserClosed As Boolean)
   StartServiceAt(upDateWalpapr, DateTime.Now + 20000, True)   ' 20 seconds for testing
End Sub
Then I read that "Even if the service is stopped, StartServiceAt will start it again. This is the whole purpose of StartServiceAt."

I have done both in myOwnService service:
B4X:
 #Region  Service Attributes
   #StartAtBoot: True
   ' #StartCommandReturnValue: android.app.Service.START_STICKY
B4X:
 Sub Service_Start (StartingIntent As Intent)
   Log("       my  Service_Started upDateWalpapr")
   StartActivity(theUpdater)                ' neither Resume the Activity alone, both seem needed
       CallSubDelayed(theUpdater, "changePicture")    ' neither Resume the Activity alone, both seem needed
   ' Starter.csu.CallSubPlus(theUpdater, "changePicture", 6000) 'DOES IT STOP SCREEN UPDATE ? ?  ' doesn't help
End Sub
B4X:
 Sub changePicture
   If Starter.gtum = Starter.lstGlbl.Size - 1 Then
       Starter.gtum = 0
       Log("                lstGlbl.Size maxed")
   Else
       Starter.gtum = Starter.gtum + 1
       Log("                lstGlbl.Size    NOT maxed")
   End If

   SetWallPaper(LoadBitmap(Starter.daPaf, Starter.lstGlbl.Get(Starter.gtum))) ' change wallpaper
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
What is the way to prevent a Service from firing when the phone is in Sleep state (the screen turns off)?
The last parameter in StartServiceAt call determines whether the service will start when the phone is sleeping. Set it to False if you don't want it to start.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
The last parameter in StartServiceAt call determines whether the service will start when the phone is sleeping. Set it to False if you don't want it to start.
Thanks. I see it now in 'Basic4android - Core.htm', and am putting that file in my 'primary Local Search' folder.
It still amazes me how dense this Basic can be.
edit: Still doing multiple wallpaper updates when manually waking phone, looking for the likely causes.
 
Last edited:
Upvote 0
Top