Time for action - using sensors in the foreground activity
In this section, we will explore how to use sensors in the activity. This is the most basic and straightforward way of using sensors. Also, it's the most efficient way if your sensor functionality only ties to that activity:
- The first step is to implement our activity with the
SensorEventListener
interface so that our activity can receiveSensorEvent
through theonSensorChanged()
method. The following code snippet shows the necessary import statements and the class declaration:import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; public class SensorActivity extends Activity implements SensorEventListener{
- Now, we will create the instance of
SensorManager...