怎么让Windows Mobile 在待机后还运行我们开发的程序

    技术2025-12-28  7

    可以通过PowerPolicyNotify(PPN_UNATTENDEDMODE, TRUE);来实现,不过请记住对应关系,及使用PowerPolicyNotify(PPN_UNATTENDEDMODE, TRUE)后必须还要使用PowerPolicyNotify(PPN_UNATTENDEDMODE, FALSE);

     

     

    系统将要待机时会先进入unattended状态如果在一段时间内没有操作接着进入待机状态关键是在系统进入unattended状态时循环调用SystemIdleTimerReset(大概30秒一次)系统就可以保持在这个状态注意:有些手机驱动不完善可能没有unattended状态static const DWORD maxMsgQueueMsgSize = sizeof(POWER_BROADCAST_POWER_INFO) + sizeof(POWER_BROADCAST) + MAX_PATH;// create stop eventHANDLE hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);// create event for GPS changeHANDLE hDeviceStateChanged = CreateEvent(NULL, FALSE, FALSE, NULL);// Create message queueMSGQUEUEOPTIONS msgQueueOptions;ZeroMemory(&msgQueueOptions, sizeof(msgQueueOptions));msgQueueOptions.dwSize = sizeof(msgQueueOptions);msgQueueOptions.dwFlags = MSGQUEUE_NOPRECOMMIT ;msgQueueOptions.cbMaxMessage = maxMsgQueueMsgSize;msgQueueOptions.bReadAccess = TRUE;HANDLE hMsgQueue = ::CreateMsgQueue(NULL, &msgQueueOptions);HANDLE hPowerNotification = ::RequestPowerNotifications(hMsgQueue, PBT_POWERINFOCHANGE | PBT_TRANSITION);// opening GPS deviceHANDLE hGPSDevice = ::GPSOpenDevice( 0, hDeviceStateChanged, NULL, 0);bool bRun = true; // run loopbool bHasSignal = false;  // change power requirement for GPS device and SD cardHANDLE hGPSPowerReq = ::SetPowerRequirement(L"GPS0:", D0, POWER_NAME|POWER_FORCE, NULL, NULL);HANDLE hPowerReq = ::SetPowerRequirement(L"DSK1:", D0, POWER_NAME|POWER_FORCE, NULL, NULL);// change to Unattended mode::PowerPolicyNotify(PPN_UNATTENDEDMODE, TRUE);// LOOOPHANDLE events[3] = {hDeviceStateChanged, hStopEvent, hMsgQueue} ;while (bRun){DWORD dwWaitRes = ::WaitForMultipleObjects(3, events, FALSE, 1000 ); // read every 1. sec.switch (dwWaitRes){case WAIT_OBJECT_0:// device status changed{GPS_DEVICE dev;ZeroMemory(&dev, sizeof(dev));dev.dwVersion = GPS_VERSION_1;if (ERROR_SUCCESS == ::GPSGetDeviceState(&dev)){// do something...  }}break;case WAIT_OBJECT_0+1:// exit requested from exit event.. this is done from other thread by calling SetEvent(hExitEvent)bRun = false;break;case WAIT_OBJECT_0+2:// if we are on low power, stop monitoring{// there is a message in EventQueue.. read queueBYTE buffer[maxMsgQueueMsgSize];DWORD NumberOfBytesRead = 0;ZeroMemory(buffer, maxMsgQueueMsgSize);DWORD dwFlags;if (::ReadMsgQueue(m_hMsgQueue, buffer, maxMsgQueueMsgSize,&NumberOfBytesRead, 0, &dwFlags ) && NumberOfBytesRead>=sizeof(POWER_BROADCAST)){POWER_BROADCAST* pPwrBrodcast = (POWER_BROADCAST*)(buffer);switch(pPwrBrodcast->Message){case PBT_POWERINFOCHANGE:{// if battery level is to low, finish with GPS readingPOWER_BROADCAST_POWER_INFO* pPowerInfo = (POWER_BROADCAST_POWER_INFO*)pPwrBrodcast->SystemPowerState;if ((pPowerInfo->bBatteryFlag==2 || pPowerInfo->bBatteryFlag==4) && (pPowerInfo->bACLineStatus==0))bRun = false;}break;case PBT_TRANSITION:{std::wstring powerState = pPwrBrodcast->SystemPowerState;// handle power statesif (powerState==L"resuming")::PowerPolicyNotify(PPN_UNATTENDEDMODE, TRUE); // basicaly should never happen, because we do not allow to go in  if (powerState==L"unattended")SystemIdleTimerReset();}break;default:ASSERT(!"Unknown message");}}break;  case WAIT_TIMEOUT:// new locationbreak;}// DO SOMETHING... e.g. Read position from GPS PORT}// ENDLOOP::PowerPolicyNotify(PPN_UNATTENDEDMODE, FALSE); // Leave unattended mode::ReleasePowerRequirement(hGPSPowerReq); // allow GPS device to go in sleep::ReleasePowerRequirement(hPowerReq);::GPSCloseDevice(hGPSDevice); // close GPS deviceStopPowerNotifications(hPowerNotification);CloseMsgQueue(hMsgQueue);CloseHandle(hPowerNotification);CloseHandle(hMsgQueue);

     

     

    转载自:http://topic.csdn.net/u/20071207/10/dd0a6bb4-4929-4eca-a7f2-59677fad404f.html?2543

    最新回复(0)