Saturday 19 October 2013

Barometer using Type.Pressure Sensor

Pressure Sensor

Create new Android Project
Project Name: Barometer
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Barometer
Package Name: com.shaikhhamadali.blogspot.barometer
Create layout file: activity_barometer



1.create layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Barometer" >

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="@string/Heading"
        android:textColor="#0835C9"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/TVAirPressure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#08C9BD"
        android:textSize="40sp" />

</RelativeLayout>

2.code of activity:
package com.shaikhhamad.blogspot.barometer;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.widget.TextView;

public class Barometer extends Activity implements SensorEventListener{
 //SensorManager lets you access the device's sensors
 //declare Variables
 TextView textView,TVAirPressure;
 private SensorManager sensorManager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_barometer);
  textView=(TextView)findViewById(R.id.TextView);
  TVAirPressure=(TextView)findViewById(R.id.TVAirPressure);
  //create instance of sensor manager and get system service to interact with Sensor
  sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
 }
 @Override
 protected void onResume() {
  super.onResume();
  // register this class as a listener for the Pressure Sensor
  sensorManager.registerListener(this,
    sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
    SensorManager.SENSOR_DELAY_NORMAL);
 }
 // called when sensor value have changed
 @Override
 public void onSensorChanged(SensorEvent event) {
  if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
   float[] values = event.values;
   TVAirPressure.setText("" + values[0]);
  }

 }
 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {

 }

 @Override
 protected void onPause() {
  // unregister listener
  super.onPause();
  sensorManager.unregisterListener(this);
 }
}

3. note that:

4. conclusion:

  • Some information about how to use TYPE_PRESSURE.
  • know what are Sensors and how to use Sensors.

5. About the post:

  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!
6. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh