?
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
Medium Priority
?
358 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 24

Accepted Solution

by:
alexey_gusev earned 1500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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