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
341 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now