views:

62

answers:

2

Hi everyone,

recently this post inspired me, I want to track my own life, too.

I take a look at my cellphone's clock every time when go to sleep or after wake up, so I need some program to hook to my cellphone's on/off button, and log the timestamp when I press it.

I am using WM6.5 on a HTC TyTN II. If there is existing software that can do this with few settings and tweaks it would be nice, but I can also write code myself. Any suggestions?

+1  A: 

This is more tricky than you could possibly imagine!

The 'on/off' button isn't directly available to the OS (i.e. it's not visible in the keypad driver), as it is hooked into a rather complex system of power collapse (basically, the Apps CPU is completely powered down when the phone is 'off' - the modem wakes up according to the network-configured paging cycle).

The wake-sleep interactions of the two CPUs are extremely complex, and prone to race conditions. Even if you managed to hook it (which would require deep kernel-level programming and a number of security hacks), you would probably make your phone very unstable.

Jeremy O'Donoghue
http://social.msdn.microsoft.com/Forums/en/windowsmobiledev/thread/e5e7d68f-ee9f-480a-98c9-907b1e6d9e29is this solution legit?
est
I haven't run the code, but it looks to be legit. Instead of trying to hook the power key directly, it registers to receive notifications of power state change events.MSDN has documentation of the exported power APIs at http://msdn.microsoft.com/en-us/library/aa917813%28v=MSDN.10%29.aspx. You are probably interested in the restore notification PBT_RESTORE.
Jeremy O'Donoghue
+1  A: 

You can use the device power notifications by P/Invoking RequestPowerNotifications to know when a power state changes (there's a CodeProject article that covers this as well). Be aware that the power down notification doesn't wait for subscribers to run code, so in most cases your application's handler doesn't actually run until the device wakes back up (meaning that if you're writing a timestamp or something you're going to get the wake time, not the sleep time).

Also be aware that different devices handle power management differently, so YMMV.

ctacke