Solved

What code can I use to prevent the screen to turn off while my application is running on Phone ?

Posted on 2009-07-10
12
354 Views
Last Modified: 2013-12-27
I have written a program in Visual Studio 2008 for a Smart Device (Windows Based).
Program is in .Net
What I want is that the screen light doesn't turn off at all while the application is running.
I suppose it is caused by the energy settings of my Mobile Device, but I want to overrule these settings.

F.E. Just like TomTom manages to do on my Smartphone.

I could really use your help here, because there's little to find about this subject.
0
Comment
Question by:karstieman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24826951
look at SetPowerRequirement() and power management in general, some example is available here:

http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesnative/thread/37ecff14-0415-4c9f-a404-e4a98078bd0e
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 24827920
0
 
LVL 5

Author Comment

by:karstieman
ID: 24831607
Thanks for the help guys !

I now have the code ( class ) and can manage the backlight to stay on.

But I want the option to give the backlight control back to the mobile device too.
Here I still have a problem, because after enabling the backlight programmatically and turning it off (what doens't work well untill now), whenever the screen turns completely off, it turns on again and off, and on, etc.

The class I use is in the code snippet.

This I use to make the backlight stay on: SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)

This is what I use to turn it off, and what doens't work completely.
ReleasePowerRequirement(Handle)


SO PLEASE HELP IN TURNING THE BACKLIGHT OFF AGAIN AND GIVE BACKLIGHT CONTROL BACK TO THE MOBILE DEVICE
Public Class Backlight
    Public Declare Function SetPowerRequirement Lib "coredll.dll" (ByVal pvDevice As String, ByVal DeviceState As PowerState, ByVal DeviceFlags _
As Integer, ByVal pvSystemState As IntPtr, ByVal StateFlags As Integer) As IntPtr
    Public Declare Function ReleasePowerRequirement Lib "coredll.dll" (ByVal handle As IntPtr) As Integer
    'ensure the power requirement is released
    Protected Overrides Sub Finalize()
        Release()
    End Sub
    'handle to the power requirement
    Public handle As IntPtr
    Public Enum PowerState
        PwrDeviceUnspecified = -1
        D0 = 0 'full on _
        D1 = 1 'low power
        D2 = 2 'standby
        D3 = 3 'sleep
        D4 = 4 'off
        PwrDeviceMaximum = 5
    End Enum
    'keep the backlight lit
    Public Sub Activate()
        'request full power
        handle = SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)
    End Sub
    'keep the backlight lit
    Public Sub DeActivate()
        'request no power
        handle = SetPowerRequirement("BKL1:", PowerState.D4, 1, IntPtr.Zero, 0)
    End Sub
    Public Function PowerSettingChanged() As Boolean
        If handle = IntPtr.Zero Then
            Return False
        Else
            Return True
        End If
    End Function
    'release power requirement
    Public Sub Release()
        If handle.ToInt32() <> 0 Then
            Dim result As Integer
            result = ReleasePowerRequirement(handle)
            handle = IntPtr.Zero
        End If
    End Sub
 
 
 
End Class

Open in new window

0
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
ID: 24831887
this SetPowerRequirement()  function was always kind of trouble :)

try attached class, I guess we use the same original code, but with few modifications here and there
Backlight.txt
0
 
LVL 5

Author Comment

by:karstieman
ID: 24833510
@Alexey Gusev:Thanks for the code !

But I'm still not able to give backlight control back to the pda...
Using the code "ReleasePowerRequirement(Handle)" nothing happens...

What line do you use to exactly for this ?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24833520
quite possible that you would also need to SendMessage(HWND_BROADCAST,WM_SETTIGNSCHANGE,0xF2,0), but usually SetSystemPowerState() worked.
0
 
LVL 5

Author Comment

by:karstieman
ID: 24838783
@Alexey Gusev: I have used your class and coed to keep backlight on works fine !
LIGHT ON --> "Backlight.SetPowerRequirement("BKL1:", DevicePowerState.D0, 1, IntPtr.Zero, 0)"

But I need the exact Vb code to turn backlight OFF again, so the Mobile Device gains control again.

I now have put it under a button for testing purposes, but it doens't work :(

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DeviceApplication1.Backlight.ReleasePowerRequirement(Handle)
End Sub

What do you use for this ?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24838845
I use

SetSystemPowerState(sysPowerStateName, flags, POWER_FORCE)

in addition to ReleasePowerRequirement(Handle) [look at Release method]
besides, try to call SendMessage() with parameters from my prev post. It all might need just proper combination of power management functions and gettting the OS to refresh its data
0
 
LVL 5

Author Comment

by:karstieman
ID: 24839034
When I use
SetSystemPowerState(sysPowerStateName, flags, POWER_FORCE)
from my application it says:

'DeviceApplication1.Backlight.Private Shared Function SetSystemPowerState(psState As String, StateFlags As Integer, Options As Integer) As Integer' is not accessible in this context because it is 'Private'.      
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24839064
well, make it public
0
 
LVL 5

Author Comment

by:karstieman
ID: 24842354
It's better if I make a new queston about this.
You helped me with me initial question, so thank you very much !
0
 
LVL 5

Author Closing Comment

by:karstieman
ID: 31602255
Thank you for your assistance !
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question