Book Image

Android Sensor Programming By Example

By : Varun Nagpal
Book Image

Android Sensor Programming By Example

By: Varun Nagpal

Overview of this book

Android phones available in today’s market have a wide variety of powerful and highly precise sensors. Interesting applications can be built with them such as a local weather app using weather sensors, analyzing risky driving behavior using motion sensors, a fitness tracker using step-counter sensors, and so on. Sensors in external devices such as Android Watch, Body Analyzer & Weight Machine, Running Speed Cell, and so on can also be connected and used from your Android app running on your phone. Moving further, this book will provide the skills required to use sensors in your Android applications. It will walk you through all the fundamentals of sensors and will provide a thorough understanding of the Android Sensor Framework. You will also get to learn how to write code for the supportive infrastructure such as background services, scheduled and long running background threads, and databases for saving sensor data. Additionally, you will learn how to connect and use sensors in external devices from your Android app using the Google Fit platform. By the end of the book, you will be well versed in the use of Android sensors and programming to build interactive applications.
Table of Contents (13 chapters)
Android Sensor Programming By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Time for action – listing the available sensors on a device


There are multiple sensors available on a device. In this section, we will learn how to get a list of all the available sensors. We will be populating the names of the available sensors in a list and will be displaying it on the screen using ListView.

  1. The following code block shows the declarations required by the activity. We don't need the SensorEventListener interface, as we will not be dealing with the values of the sensor. We declare ListView.ListAdapter, and SensorManager, along with the list of Sensor Objects to populate the list:

           public class SensorListActivity extends Activity 
           implements OnItemClickListener{ 
     
             private SensorManager mSensorManager; 
             private ListView mSensorListView; 
             private ListAdapter mListAdapter; 
             private List<Sensor> mSensorsList; 
    
  2. In the onCreate() method, we instantiate our SensorManager, ListView, and ListAdaptor...