Screen ON or OFF on service start

salmander

Active Member
Licensed User
Longtime User
Hey all,

How can I determine if the screen is on or off when the service starts?

I have tried Phone events and listened to the screen on and off events, but the event will not trigger if the screen is already in that state. For e.g:

If the screen is off and the service gets started by startServiceAt(service_to_start), and in the service there are screen on and off events. But the screen_off event will not trigger because the screen is already in that state.

Can anyone help me please?


Thanks.
 

margret

Well-Known Member
Licensed User
Longtime User
If your screen on events always trigger when the screen is on and your screen off events always fire when the screen is off. You, can set a global variable and in each event of screen on and screen off, set this variable:

Example:

B4X:
Sub Globals
      Dim scrvar As Boolean
End Sub

Sub ScreenOn
      scrvar = True
      'your other code
End Sub

Sub ScreenOff
      scrvar = False
      'you other code
End Sub
 
Upvote 0

salmander

Active Member
Licensed User
Longtime User
If your screen on events always trigger when the screen is on and your screen off events always fire when the screen is off. You, can set a global variable and in each event of screen on and screen off, set this variable:

Example:

B4X:
Sub Globals
      Dim scrvar As Boolean
End Sub

Sub ScreenOn
      scrvar = True
      'your other code
End Sub

Sub ScreenOff
      scrvar = False
      'you other code
End Sub
Thanks margret but I am using this in a service so there is process global variables. but the scarvar would not return the correct value if the service is just created, as I mentioned in my post I have to determine the value of screen on/off in service start sub.
 
Last edited:
Upvote 0

salmander

Active Member
Licensed User
Longtime User
Sorry mate I think you misunderstood me. In service_start, I am calling a sub called performActions. This sub performs various actions, but it needs to know if the screen state at that moment is off or on.

The screen_on and screen_off events will trigger if it detects any change in the screen activity, i.e: if the screen turns on then the screen_on will trigger, if the screen turns off then the screen_off will trigger. But this would not tell the current state of the screen.
 
Upvote 0

salmander

Active Member
Licensed User
Longtime User
I don't want to turn the screen on mate. I want to know the current state of the screen. Thanks still.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
I have looked around and as far as I can see it looks like you will have to have a library for this. It seems it's not easy to do and requires a page or two of java code for the library. Someone that knowns more than I may have different information for you. Sorry I could not help.:(

Handling Screen OFF and Screen ON Intents « Think Android
 
Last edited:
Upvote 0

salmander

Active Member
Licensed User
Longtime User
I have looked around and as far as I can see it looks like you will have to have a library for this. It seems it's not easy to do and requires a page or two of java code for the library. Someone that knowns more than I may have different information for you. Sorry I could not help.:(

Handling Screen OFF and Screen ON Intents « Think Android
Its okay mate. Thanks for your effort. I am sure someone will come up with a solution.
 
Upvote 0

salmander

Active Member
Licensed User
Longtime User
Thanks Erel, I have tried that, but through this method it only gives the change in screen on/off events. I want to find the current state of the screen, either its turn on or off. I was looking on android developer forum and found this;
public boolean isScreenOn ()

Since: API Level 7
Returns whether the screen is currently on. The screen could be bright or dim.
B4X:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 boolean isScreenOn = pm.isScreenOn();
URL
Can I use it using reflector library please?
 
Upvote 0
Top