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
349 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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