Keep Windows Mobile App running in standby mode

windows-mobile

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

Best Answer

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);
Related Topic