Mobile devices have sensors that can report all sorts of information about their own usage and the world around them. Position sensors report the device’s location, environment sensors can report atmospheric conditions, and motion sensors report the device’s changes in position or orientation. You can listen for this information using classes from the package android.hardware. In this example I’ll describe how to use one of the most common sensors, the accelerometer, which reports changes in the device’s orientation.
- Mobile devices have sensors…that can report all sorts of information…about their own state and the world around them.…Position sensors report the device's location,…environment sensors can report atmospheric conditions,…and motion sensors report the device's changes…in position or orientation.…You can listen for this information…using classes from the package "android.hardware".…I'll go to the API docs and search for that package.…
This package includes interfaces and classes…for all sorts of sensors,…including cameras, accelerometers and much more.…I'll demonstrate how to use…one of the most common sensors, the accelerometer,…which reports changes in the device's orientation.…This project, "Sensors", is my basic code runner app.…It starts off with a couple of buttons at the bottom,…and when I touch the Run Code button…it simply displays a message.…
I'm going to add code to the MainActivity class…to detect when the device is shaken by the user,…and I'll respond by displaying a message on the screen.…I'll start in the class declaration.…
Watch these Java tutorials to learn smarter, more efficient methods for Android app development.
- Preparing the development environment
- Packaging and running Android apps
- Optimizing Java code for Android
- Implementing event handler interfaces
- Defining custom callback methods
- Working with the Android SDK's Java packages
- Storing data
- Reading text files
- Parsing JSON and XML data
- Managing device sensors
- Playing audio