I’ve been doing Windows Phone development on and off for the past couple of years. In fact, I recently published the first version of my application “Snap Receipt” to the Windows Phone Marketplace. It’s an easy way to get receipts from your phone into Concur’s expense reporting system. You can check it out here. During my experiences with Windows Phone development I’ve identified 10 essential things developers need to know if they’re going to starting writing code against the platform. I thought I’d take some time to share them with you over a series of posts. Please note, these posts refer to Windows Phone 7.0/7.1. I have yet to see any developer bits for Windows Phone 8, but it is my hope that many of the principles I discuss in this series apply to the new platform as well.
In my last post in this series I discussed Choosers. In this post I’ll look at the Accelerometer.
So, what is an accelerometer? Simply, put, it’s a sensor that is part of the device’s hardware that provides data about device movement that can you can use in your applications. An easy way to understand this in terms of Windows Phone, is to think about a windows phone page that supports both portrait and landscape mode. When you move the phone from portrait view to landscape view, the page automatically changes rotation for the new view. This is accomplished via the accelerometer.
When working with the accelerometer, there are two core classes you need to be aware of, as well as one enumeration. All of which reside in the Microsoft.Devices.Sensor namespace.
The first class is the Accelerometer class. This class gives your application access to the device’s accelerometer.
The next class is the AccelerometerReadingEventArgs class. When handling the accelerometer’s reading changed event, you can use this class to get information about the acceleration that has occurred. Specifically you can get access to the acceleration that has occurred in the x, y, and z direction in terms of gravitational units.
Finally, you should also be familiar with the SensorState enumeration. This enumeration contains values for the current state of the device’s accelerometer. There are a number of potential values, including whether or not the sensor is ready, if it can’t obtain data, if the application doesn’t have permission to access it, etc. This is exposed via the accelerometer’s state property.
Next up in this series we’ll look at the different ways you can support gestures in your Windows Phone applications.
Previous posts in this series