Android Sensor Tutorial – Mobile App Development, Android App Development, iphone App Development class="post-template-default single single-post postid-2433 single-format-standard wp-custom-logo"

Android Sensor Tutorial

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. These sensors are capable of providing raw data with high precision and accuracy, and are useful if you want to monitor three-dimensional device movement or positioning, or you want to monitor changes in the ambient environment near a device. For example, a game might track readings from a device’s gravity sensor to infer complex user gestures and motions, such as tilt, shake, rotation, or swing. Likewise, a weather application might use a device’s temperature sensor and humidity sensor to calculate and report the dewpoint, or a travel application might use the geomagnetic field sensor and accelerometer to report a compass bearing.

The Android platform supports three broad categories of sensors:

  • Motion sensors

    These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.

  • Environmental sensors

    These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.

  • Position sensors

    These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers. You can access sensors available on the device and acquire raw sensor data by using the Android sensor framework. The sensor framework provides several classes and interfaces that help you perform a wide variety of sensor-related tasks.

One of the most interesting topics in my opinion is how to
use Sensor in Android. Nowadays our smartphone are full of sensors and we can use it to control somehow our app. The most common sensors are:

  • GPS

  • 1. Proximity sensor
  • 2. Light sensor
  • 3. Temperature sensor
  • 4. Barometer sensor
  • 5. NFC

just to mention some of them. In this post we will explain how to obtain a list of sensor

We can retrieve the sensor list too and we need a bit of code.

Some of the sensors are hardware based and some are software based sensors. Whatever the sensor is , android allows us to get the raw data from these sensors and use it in our application. For this android provides us with some classes.

Android provides SensorManager and Sensor classes to use the sensors in our application. In order to use sensors , first thing you need to do is to instantiate the object of SensorManager class. It can be achieved as follows.

[code lang=”js”]
SensorManager sMgr; sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);

The next thing you need to do is to instantiate the object of Sensor class by calling the getDefaultSensor() method of the SensorManager class. Its sytanx is given below

[code lang=”js”]
Sensor light; light = sMgr.getDefaultSensor(Sensor.TYPE_LIGHT);

Once that sensor is declared , you need to register its listener and override two methods which are onAccuracyChanged and onSensorChanged. Its syntax is as follows

[code lang=”js”]
sMgr.registerListener(this, light,SensorManager.SENSOR_DELAY_NORMAL); public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { }

Getting list of sensors supported.

You can get a list of sensors supported by your device by calling the getSensorList method , which will return a list of sensors containing their name and version number and much more information. You can then iterate the list to get the information. Its syntax is given below:

[code lang=”js”]
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE); List<Sensor> list = sMgr.getSensorList(Sensor.TYPE_ALL); for(Sensor sensor: list){ }


[code lang=”js”]
package com.example.sensors; import java.util.List; import; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private SensorManager sMgr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView sensorsData = (TextView)findViewById(; sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE); List list = sMgr.getSensorList(Sensor.TYPE_ALL); StringBuilder data = new StringBuilder(); for(Sensor sensor: list){ data.append(sensor.getName() + "\n"); data.append(sensor.getVendor() + "\n"); data.append(sensor.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(, menu); return true; } }


[code lang=”js”]
<RelativeLayout xmlns:android="" xmlns: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>


< ?xml version="1.0" encoding="utf-8"?> Sensors Settings Hello world! List of sensors supported


[code lang=”js”]
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="" package="com.example.sensors" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sensors.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>

Let’s try to run our Sensor application we just modified. I assume you had created your
AVD while doing environment setup. To run the app from Eclipse, open one of your project’s activity files and click

Android Sensor Tutorial

Warning: _() expects exactly 1 parameter, 2 given in /home2/mobilemeri7/public_html/wp-content/themes/mobilemerit/comments.php on line 28

Write a Reply or Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Cracked Accounts Free Netflix Accounts VPN Proxy Shares Hack Programları Hack dökümanları Hack methodsları Wordlist Combolist Warez Scriptler Download Warez Files WhatsApp Numara çekme +90 Abazalardan Para kazanma Telegram Mavi tik alma 2022 Numara sorgulama Azuere Aws Sanal Kart Method WhatsApp vurma Güncel Method 2022 Bedava .Com .Org .Net VDS Premium Host YouTube Sahte Telif Methodu Tıklama hilesi ile para kazanmak Azure Hack method Pubg Mobile Uc Almak Hedef Kişinin herşeyini öğrenmek instagram Telif taslağı 2022 Ölüm Belgesi ile instagram hesap kaptma instagram form ile ebedi hesap kapatma instagram taklitsiz hesap kapatma instagram Çift faktör geçme instagram tiktok hesap çalma method instagram 2 Faktörlü doğrulama geçme instagram hesap kapatma 2022 instagram rever kırmak instagram Revert Kırma Abaza Dayı Methodu Aylık Dolar kazamak 1500 dolar instagram Çalmak istiyorum instagram düşmeyen takipçi instagram hackleme sitesi Whatsapp Kapatma %100 Abaza tokatlayarak para kazanmak Spotify ile para kazanma Günde 100 tl kazanma sınırsız kazanç Yatakta yatarak para kazanma YouTube izlenme arttırme yöntemi Oturduğun yerde para kazanma yöntemi Discord Hack tool Discord hack programları Cerebrus Rat v2 kodları PP Takipçi Çeken Telif Scripti Facebook profil kapatma methodu 2022 instagram yapay ile banned methodu instagram hesap satşı Sosyal medya hesap satışı Sosyal medya hack Leak Forum Crack Forum Cracking Forum Warez Forum Hack Forum illegal Forum Hacking Forum instagram Forum instagram Forumu Hacklink Hack forum php Shell indir Hack haber nakliyatankara nakliyatistanbul nakliyatankara nakliyatJigolodiyarijigolo sitesigerçek jigolo sitesijigolo sitelerimjigolo şirketlerijigolo sitelerijigolo başvurujigolo arayan kadınlara ulaşKocuceKo cuce