Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
And finally let's take a look at how to use Accelerometer. Once again, back to our Solution Explorer, let's open up Accelerometer page. And here, similar to the GPS, we have a button which Starts Tracking the Accelerometer data, because again it's Event driven, and you do not want to be tracking it all the time unless you are actually using it, partly to save battery life. And then we have three text blocks where we are going to show what the current values of the three axes of the Accelerometer are as being reported by the Accelerometer. So let's go take a look at the code behind this.
In our Accelerometer page, in order to be able to access the Accelerometer, we need to get access to the Microsoft.Devices.Sensors API, where there is an Accelerometer object. Then we need a Timer because we use the Timer in order to update the UI as opposed to using a BeginInvoke that we used in the GPS thing. It's just a different way of getting data updated on the UI thread. So instead of taking the actual Update events and firing off for UI thread, we simply update the UI thread based on the Timer. The result of an Accelerometer Readback is an object of type Vector3, so you got X, Y, and Z axes.
And then, each time we get an Accelerometer result, we get to find out whether or not the Accelerometer data is valid. So we start up by saying, you know, do we have an Accelerometer on this device? Well, all existing Windows Phones have Accelerometers, but it is possible that you might eventually have one that doesn't. And assuming that we do have one, then we initialize our Timer or set it to 30 Milliseconds and set up an EventHandler for it, but we do not actually start the timer. Accelerometer starts when the user taps the button. First time through we go to see if we have actually allocated our Accelerometer object.
If not, we allocate it, we set up time between updates of 20 Milliseconds, and again we set up an EventHandler for the current value changed event, and then we Start the Accelerometer, and we Start our Timer. It is highly unlikely that there would be any problem with that, but just in case, we cache the exception to make sure that the Accelerometer actually did start. Here is our callback method, all we do is set the dataOK flag and set the Vector3 whatever the current reading for the Accelerometer is. And then we access that data in our Timer callback, going and looking at if the data is okay, then we look at the X, Y, and Z Properties on that Vector3 object and set the text properties of our text blocks to update the current X, Y, and Z value.
And that is a brief overview of the different kinds of sensors that you can access on every Windows Phone. Some newer Windows Phones also have a Compass, and in the future there may very well be other kinds of sensors that may show up. So stay tuned to the Microsoft web site that gives you information on how to access the detailed sensors, especially the how-to site, it always has samples for each kind of capabilities that are available in Windows Phone devices.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104276 Viewers
56 Video lessons · 116204 Viewers
71 Video lessons · 85422 Viewers
131 Video lessons · 40901 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.