views:

3521

answers:

4

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

+4  A: 

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)
ratchetr
A: 

Thanks for that. Could you please explain what you mean by eventName here? I had a look on msdn and according to it an Application's name has to be passed.

Sandeep

I see what you mean. MSDN is a moving target. At least in this version: http://msdn.microsoft.com/en-us/library/aa931253.aspx someone has added a correction.See also: http://www.archivum.info/microsoft.public.pocketpc.developer/2007-10/msg00579.html
ratchetr
I had a look at those links but I'm a bit lost here. Could you please give a example as to how I should do this.Sandeep
Sandeep -What language is your app in? I can post some C++ code, but if the app is VB or C#, it might not be very useful.
ratchetr
C++ code would be fine. I am using Lazarus/ Object Pascal.Sandeep
+3  A: 

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);
JMD
+2  A: 

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.

vividos