14 September 2017

Get The List of Sensors Available in Phone

In this Example of Android, We will See How to Get the List of Sensors which are available in Phone
in Today's world where Apps is Developing with So Much Of advanced Features, Many Apps Are Presents in Play store which uses the Sensors Which are available in Phone To Get some Real-time Data and Make Some Calculation with That and show some Output.

For e.g:-  use of IR-based proximity sensors This sensing is done for: Reduce display power consumption by turning off the LCD backlight and to disable the touchscreen to avoid inadvertent touches by the cheek.

In This Tutorial We will Use List-view To Show All Available Sensor in Phone So if You Don't Know About List View Please Checkout My Previous Tutorial By Clicking Here.


Below is The Screen Shot of Our Final Output


Final Output Of Getting List of Sensors Available in Phone




1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your First activity as MainActivity.


2. Once the project is created open your activity_main.xml file and Add Below Code.

 
<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:paddingLeft="64dp"  
   android:paddingRight="64dp"  
   android:paddingTop="64dp"  
   android:paddingBottom="64dp"  
   tools:context="com.targetandroid.info.sensorlist.MainActivity">  
   <ListView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/lvsensors">  
   </ListView>  
 </RelativeLayout>  


3. Now Open Your MainActivity.java

   SensorManager lets you access the device sensors

   Add Below Code In File


package com.targetandroid.info.sensorlist;  

 import android.hardware.Sensor;  
 import android.hardware.SensorManager;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.Menu;  
 import android.view.MenuItem;  
 import android.widget.ArrayAdapter;  
 import android.widget.ListView;  
 import java.util.ArrayList;  
 import java.util.List;  

 public class SensorsActivity extends AppCompatActivity {  
   //Creating Variable  
   ListView lvsensors;  
   SensorManager mSensorManager;  

   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main); 
 
     lvsensors=(ListView)findViewById(R.id.lvsensors);  
     mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);  

     //Getting the list of all sensors supported by device  
     List<Sensor> sList=mSensorManager.getSensorList(Sensor.TYPE_ALL); 
 
     List<String> namelist= new ArrayList<>(); 
 
     //put all the name of sensors into new List<String>  
     for(int i=0; i<sList.size();i++){  
       namelist.add(sList.get(i).getName());  
     }  

     ArrayAdapter listadapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,namelist);  
     lvsensors.setAdapter(listadapter);  
   }  
 }  


4. Now finally run Your Project

You Will See A Below Output

Final Output Of Getting List of Sensors



For More Tutorial Share And Subscribe to my Blog So you will get all the updates on your email, We Never Spam.

Thank You

Sayyed Faizan is hardcore Android Developer.he always a proactive in improving the situation rather than just playing the victim when things go wrong.he think creatively/outside of the box.


EmoticonEmoticon