TABLE OF CONTENTS

Chapter 1

Retrieving an Image, Scale It and Display on Canvas

Retrieving an image from a drawable folder. Then, scaled it and display it on the canvas.

Chapter 2

Android SensorManager

Whether you are developing a compass or GPS apps, you need to show the user the direction. Therefore, accessing the data from the sensor of a device is necessary. The following codes show you how to get the sensor data. The SensorManager is used for the above purpose.

Android Programming

Last Update: 2019-04-05

Writing my experience and study notes


Android SensorManager

Chapter 2- Android Programming

Last Update: 2019-04-05

Description
Whether you are developing a compass or GPS apps, you need to show the user the direction. Therefore, accessing the data from the sensor of a device is necessary. The following codes show you how to get the sensor data. The SensorManager is used for the above purpose.

Content

Whether you are developing a compass or GPS apps, you need to show the user the direction. Therefore, accessing the data from the sensor of a device is necessary. 

The following codes show you how to get the sensor data. The SensorManager is used for the above purpose.  

//Implements SensorEventListner
public class MainActivity extends AppCompatActivity implements SensorEventListener {

//define the SensorManager as senroManager
private SensorManager sensorManager;


 @Override
    protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView
   (R.layout.activity_main);

//getting the System Service, the type of service is declared in the bracket.
sensorManager = (SensorManager) 
getSystemService(SENSOR_SERVICE);

}


 @Override
    protected void onResume() {
        super.onResume();

   //the sensor resume, register the listener
   sensorManager.
    registerListener(this,
       mSensorManager.getDefaultSensor
        (Sensor.TYPE_ORIENTATION),
        SensorManager.
        SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // to stop the listener        
        sensorManager.
        unregisterListener(this);

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        
       // get the degree and round it to be float value
        float degree = Math.round(event.values[0]);

       //Assign the degree value to the variable in customCanvas
customCanvas.currentBearing
=degree;

       //redraw the canvas
        customCanvas.invalidate();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
         //Normally the accuracy is stable. So, this function isn’t used
    }


}

Share: