Skip to content

Spoke Right

Education for everyone

https://spokeright.com
Primary Menu
  • Home
  • Our Services
    • Translation
    • Interpretation
    • Transcription
    • Voice-Over
    • Proofreading
    • Content writing
  • Free Education
    • Learn Android Studio
    • Learn Python
    • Learn MongoDB
    • Learn MySql
    • Learn React
    • Montessori Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
    • TicTac
    • Checkers
    • Foosball
    • Billiards
    • Master Chess
    • Logic Game
  • Contact Us
Download Now
  • Home
  • Learn Android Studio
  • How can we work with sensors Android Tutorial
  • Learn Android Studio

How can we work with sensors Android Tutorial

Spoke Right November 22, 2022 2 min read

Sensor Android Tutorial

To monitor the three-dimensional device movement or to monitor the change in the environment of the device, the Sensors are used. In Android, we can work with different types of sensors, using the Sensor API.

Types of Sensors:

There are three types of sensors in Android. These are:

  1. Motion Sensors: To measure the acceleration forces and rotational forces along with three axes, the Motion Sensors are used in Android.
  2. Position Sensors: To measure the physical position of the device, the Position Sensors are used in Android.
  3. Environmental Sensors: To measure the environmental changes such as temperature, humidity, etc., the Environmental Sensors are used in Android.

Android Sensor API:

There are many classes and interfaces in the Android sensor API. We are going to discuss some of the important classes and interfaces of the sensor API.

SensorManager class:

To get sensor instances, to access and list sensors, and to register and unregister the sensor listeners and to serve many more such purposes, the methods of the android.hardware.SensorManager class are used. We can call the method getSystemService() and pass the SENSOR_SERVICE constant in it, to get the instance of the SensorManager.

Syntax:

SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);

Sensor class:

To get the information about the sensor such as sensor name, sensor type, sensor resolution, sensor type, etc., in Android the android.hardware.Sensor class is used.

SensorEvent class:

The instance of the SensorEvent class is created by the system to provide information about the sensor.

SensorEventListener interface:

To get the information when the sensor values or sensor accuracy changes, two call back methods are provided by the SensorEventListener interface. These are:

  • void onAccuracyChanged(Sensor sensor, int accuracy): When the sensor accuracy is changed, this method is called.
  • void onSensorChanged(SensorEvent event): When the sensor values are changed, this method is called.
  • Example:

    In the below example, we are using the Android Sensor API to print the x, y, and z axes values.

activity_main.xml:

In the activity_main.xml file, we will add a TextView.

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity"
    android:transitionGroup="true">
 
    <TextView android:text="Sensor " android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:textSize="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example"
        android:id="@+id/textView"
        android:layout_below="@+id/textview"
        android:layout_centerHorizontal="true"
        android:textColor="#0009ff"
        android:textSize="35dp" />
 
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
 
</RelativeLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to print the values of the x axis, y axis, and z axis.

package com.example.radioapp;
 
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
 
import android.widget.TextView;
 
import java.util.List;
import android.hardware.Sensor;
import android.hardware.SensorManager;
 
public class MainActivity extends Activity {
    TextView tv1=null;
    private SensorManager mSensorManager;
    @Override
 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tv1 = (TextView) findViewById(R.id.textView2);
        tv1.setVisibility(View.GONE);
 
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> mList= mSensorManager.getSensorList(Sensor.TYPE_ALL);
 
        for (int i = 1; i < mList.size(); i++) {
            tv1.setVisibility(View.VISIBLE);
            tv1.append("\n" + mList.get(i).getName() + "\n" 
+ mList.get(i).getVendor() + "\n" + mList.get(i).getVersion());
        }
    }
 
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radioapp">
 
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
 
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
 
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
 
            </activity>
 
        </application>
    </manifest>

Continue Reading

Previous: How to connect android Wifi
Next: What do you mean by Android simple graphics

Related Stories

How to Get Imageview From Sqlite Database Android Studio
1 min read
  • Learn Android Studio

How to Get Imageview From Sqlite Database Android Studio

November 28, 2022
How do you show a toast on Android
1 min read
  • Learn Android Studio

How do you show a toast on Android

November 28, 2022
How do I completely Android Java Close App
1 min read
  • Learn Android Studio

How do I completely Android Java Close App

November 27, 2022

This AD Will support Us

Support Us

Coding Ustad LTD Support
Coding Ustad LTD Support

Coding Ustad LTD

Coding Ustad LTD
Coding Ustad LTD

Recent Posts

  • Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes
  • How to Install Node.js and npm on CentOS 7
  • How to Install Node.js and npm on CentOS 7
  • How to Install CentOS Web Panel (CWP) on CentOS 7
  • The Power of Marketing in Real Estate: A Guide to Boost Your Business

Get free Hosting

Hostens.com - A home for your website
Ahsan Nadeem

You may have missed

Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes
2 min read
  • Spoke Right News

Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes

July 18, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 3, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 2, 2023
How to Install CentOS Web Panel (CWP) on CentOS 7
3 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install CentOS Web Panel (CWP) on CentOS 7

May 2, 2023
  • Home
  • Our Services
  • Free Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
  • Contact Us
Copyright © All rights reserved. | MoreNews by AF themes.