Pages

Wednesday, July 29, 2009

Windows Mobile 6: Keep Display On

Статья опубликована на Experts-Echange.
For the application that extensively works with the data such as a map or GPS, it is important to keep the display on all the time. Few years ago the function SystemIdleTimerReset() did its job. On our side we needed to detect the device idle time and call this function appropriately. In Windows Mobile 6 this function keeps the system running, but the display is turned off. So the problem is to keep the backlight on.

Of course the solution was found. If you need to keep your device at full power just use the following code:

HANDLE hBacklight = SetPowerRequirement(_T("BKL1:"), D0, POWER_NAME, NULL, 0);


Do not forget to release handle when you close application:

ReleasePowerRequirement(hBacklight)


SystemIdleTimerReset() should be used also – this function keeps the device running. If you will forget about it, the display will be on, but the device will “go to sleep” together with your application. It will look like your application gets stuck.
MSND article "Program Applications to Turn the Smartphone Backlight Off and On" explains it with more details and has a working example.
Bruce Eitman proposed new solution in his article “Windows CE: Keeping the Backlight On”. He found out that the power manager is monitoring a named event. The name of this event can be obtained from the registry:

const static LPCWSTR s_szGWE_REG_PATH = L"SYSTEM\\GWE";
const static LPCWSTR s_szActivityKey = L"ActivityEvent";
DWORD EventName(LPWSTR& lpszName)
{
HKEY hKey = NULL;
DWORD nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, s_szGWE_REG_PATH, 0, 0, &hKey);
if (nResult != ERROR_SUCCESS)
return 0;

DWORD nNumBytes = 0;
DWORD nType;
nResult = RegQueryValueEx(hKey, s_szActivityKey, NULL, &nType, NULL, &nNumBytes);
if (nResult == ERROR_SUCCESS)
{
nResult = 0;
if (nNumBytes > 0)
{
lpszName = (LPWSTR)malloc(nNumBytes + 2);
if (lpszName != NULL)
{
ZeroMemory(lpszName, nNumBytes + 2);
nResult = RegQueryValueEx(hKey, s_szActivityKey, NULL, &nType, (LPBYTE)lpszName, &nNumBytes);
nResult = wcslen(lpszName);
}
}
RegCloseKey(hKey);
}
return nResult;
}



If we have the name, we can create this event:

HANDLE hPowerEvent = CreateEvent(NULL, FALSE, FALSE, lpszEventName);


and raise it periodically:

SetEvent(hPowerEvent);

No comments:

Post a Comment