问题
I have a simple Windows Mobile application which records GPS coordinates every 5 minutes. The problem is the app works fine as long as the screen is on, as soon as the phone goes in standby mode the app stops working. When I switch on the device the app again starts working again.
What do I do to keep the app working even in standby mode?
Sandeep
回答1:
My experience with GPS is that it takes a while to get a fix (at least on my device), so I think you have to keep the phone from suspended state all the time. When I’ve been playing around with my device I’ve noticed that I have to use the built in music player to get a fix while the screen is off. As ratchetr pointed out PowerPolicyNotify(PPN_UNATTENDEDMODE,TRUE) seems to be the right way to prevent the "music player requirement".
Edit: It also seems like you have to use SetPowerRequirement / ReleasePowerRequirement on some devices.
Here is a C# sample:
public const int PPN_UNATTENDEDMODE = 0x0003;
public const int POWER_NAME = 0x00000001;
public const int POWER_FORCE = 0x00001000;
[DllImport("coredll.dll")]
public static extern bool PowerPolicyNotify(int dwMessage, bool dwData);
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr SetPowerRequirement(string pvDevice, CedevicePowerStateState deviceState, uint deviceFlags, string pvSystemState, ulong stateFlags);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int ReleasePowerRequirement(IntPtr hPowerReq);
public enum CedevicePowerStateState : int
{
PwrDeviceUnspecified = -1,
D0 = 0,
D1,
D2,
D3,
D4,
}
//Keep the GPS and device alive:
PowerPolicyNotify(PPN_UNATTENDEDMODE, true)
IntPtr gpsPowerHandle = SetPowerRequirement("gpd0:", CedevicePowerStateState.D0, POWER_NAME | POWER_FORCE, null, 0);
//Call before exiting your app:
ReleasePowerRequirement(gpsPowerHandle);
PowerPolicyNotify(PPN_UNATTENDEDMODE, false);
回答2:
Look at the CeRunAppAtTime function. Pass it a named event and the time you want to run. Wait for the event in a thread. You will want to call PowerPolicyNotify when you wake up, otherwise the device might suspend again before you finish.
The code would look something like this
CeRunAppAtTime(eventName,now + 5 minutes)
while(!quit)
WaitForSingleObject(event,timeout)
PowerPolicyNotify(PPN_UNATTENDEDMODE,TRUE)
DoGpsStuff()
CeRunAppAtTime(eventName,now + 5 minutes)
PowerPolicyNotify(PPN_UNATTENDEDMODE,FALSE)
回答3:
Maybe the answer to this SO question is of help: How can I run code on Windows Mobile while being suspended? It uses the "unattended" mode to keep the application running with screen switched off.
来源:https://stackoverflow.com/questions/873816/keep-windows-mobile-app-running-in-standby-mode