Android Code Snippet ✅ Catching screen rotation in services with a timer

SubName: Screen rotation in services, especially widgets.
Description: Sometimes you need to catch the screen rotation in services and especially widgets. Here is one solution.
B4X:
Sub Process_Globals
  Dim CheckScrOrientation, StoredOrientation As String
  Dim RotateTimer As Timer
End Sub

Sub Service_Create
  RotateTimer.Initialize("SDTime", 500)  '1000 Milliseconds = 1 second
  RotateTimer.Enabled = True
End Sub

Sub SDTime_Tick
  If GetDeviceLayoutValues.Width > GetDeviceLayoutValues.Height Then
      CheckScrOrientation = "Landscape"
  Else
      CheckScrOrientation = "Portrait"
  End If

  If CheckScrOrientation <> StoredOrientation Then
      StoredOrientation = CheckScrOrientation
      'PUT WHATEVER UPDATE/REFRESH ROUTINE OR CALLSUB YOU NEED HERE...
  End If
End Sub

Tags: catch, screen, rotation, service, widget
 
Last edited:

Scotter

Active Member
Licensed User
Wow this is exactly what I was looking for!
Does Service_Create begin on its own or I need to call it to begin the timing?
Sorry I realize this is probably a dumb question. Like most likely it needs to be called. Just making sure.
I'm very new to this language. And this is the first I've heard of "Services".
Thank you!
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Does Service_Create begin on its own or I need to call it to begin the timing?
Hello,
You would have to learn more about services in the next days
Service_Create is the sub called when you start a service that was not already started (so not already created). So, it will be called naturally when you will start the service.

The code snippet was provided before the introduction of the Starter service. Perhaps now could you put the code inside it ? (the Starter service is started each time your app (activities or services) need it).
 

Scotter

Active Member
Licensed User
Thank you!
Are you saying I can use what I have and just call Sub Service_Create?
Or that I must use the Starter service?
I'll start looking around on these forums to see if I can find more info about Services.
Thank you!
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Are you saying I can use what I have and just call Sub Service_Create?
Not at all : I am trying to explain that you can just start a service. For example, if you use the Starter service, it will be first created (the code inside Service_Create will be executed), then started (the code inside Service_Start will be executed) when your app is launched.
More information about the Starter service could be found here : https://www.b4x.com/android/forum/t...-consistent-single-entry-point.57599/#content

It is not a good idea to call the Service_Create's sub by yourself as it is a some sort "system" sub executed when the service is created. But you could copy the code above inside your Starter's service and you'll see it working immediately when your app starts

When you need to act with a "standard" service, you would use the commands listed here : https://www.b4x.com/android/help/core.html#keywords_startservice
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…