articles

Home / DeveloperSection / Articles / Sensor in Android

Sensor in Android

Manoj Pandey3346 11-Mar-2015

Use sensors on the device to add rich location and motion capabilities to your app, from GPS or network location to accelerometer, gyroscope, temperature, barometer, and more.

Most of the android devices have built-in sensors that measure motion, orientation, and various environmental condition. The android platform supports three broad categories of sensors.

·     Motion Sensors

·     Environmental sensors

·     Position sensors

Sensors can be used to monitor the three-dimensional device movement or change in the environment of the device.

Android provides sensor api to work with different types of sensors.

Sensor in Android

 

Motion Sensors-:  These are used to measure acceleration forces and rotational forces along with three axes.

Position Sensors-: These are used to measure the physical position of device.

Environmental Sensors-: These are used to measure the environmental changes such as temperature, humidity etc.

In this article I am going to create sample of sensor which shows sensor ability on our device

1.      Create an android project

2.      Add views in activity_main.xml file

<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"

   android:paddingBottom="@dimen/activity_vertical_margin"

   android:paddingLeft="@dimen/activity_horizontal_margin"

   android:paddingRight="@dimen/activity_horizontal_margin"

   android:paddingTop="@dimen/activity_vertical_margin"

   tools:context=".MainActivity" >

 

   <ScrollView

      android:id="@+id/scrollView1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_alignParentLeft="true"

      android:layout_alignParentTop="true"

      android:layout_marginLeft="16dp"

      android:layout_marginTop="16dp" >

 

   <LinearLayout

      android:layout_width="match_parent"

      android:layout_height="match_parent"

      android:orientation="vertical" >

 

   <TextView

      android:id="@+id/textView1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="Medium Text"

      android:textAppearance="?android:attr/textAppearanceMedium" />

 

      </LinearLayout>

   </ScrollView>

 

</RelativeLayout>

3.       My string resource

<?xml version="1.0" encoding="utf-8"?>

<resources> 

    <string name="app_name">AndroidSensorSample</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

      <string name="list">List of sensors supported</string> 

      </resources>

</RelativeLayout>

 

4.       Add following code in MainActivity.class

package com.example.androidsensorsample; 

import java.util.List; 

import android.hardware.Sensor;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.TextView;

 

public class MainActivity extends Activity {

                // SensorManager lets you access the device's sensors

                private SensorManager sMgr;

                List list;

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                                TextView sensorsData = (TextView) findViewById(R.id.textView1);

                                sMgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);

                                list = sMgr.getSensorList(Sensor.TYPE_ALL);

                                StringBuilder data = new StringBuilder();

                                for (int i = 0; i < list.size(); i++) {

                                                data.append(((Sensor) list.get(i)).getName() + "\n");

                                                data.append(((Sensor) list.get(i)).getVendor() + "\n");

                                                data.append(((Sensor) list.get(i)).getVersion() + "\n");

                                }

                                sensorsData.setText(data);

                }

                @Override

                public boolean onCreateOptionsMenu(Menu menu) {

                                // Inflate the menu; this adds items to the action bar if it is present.

                                getMenuInflater().inflate(R.menu.main, menu);

                                return true;

                }

}

 Now run your application

 Sensor in Android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By