List All Sensor In Android App

List All Sensor in Android 

MainActivity.java

import java.util.ArrayList;
import java.util.List;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
//variables
private ListView listView;
private SensorManager mSensorManager;
//list of sensors
private List<Sensor> deviceSensors = null;
//list of sensors names
private List<String> deviceSensorsList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create instance of list view
listView = ((ListView) findViewById(R.id.listView1));
//create instance of sensor manager and get system sensor service
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//get list of all types of sensors in you device
deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
/*you can get specific sensors by selecting type in getSensorList(type you want);*/
for(Sensor s: deviceSensors){
//get names of all the sensors in your device and add into list
deviceSensorsList.add(s.getName());
}
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, deviceSensorsList));
}
}
activity_main.xml

<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=".SensorsList" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Of All The Sensors in Your Device"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="italic"
android:typeface="sans" />

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerInParent="true" >
</ListView>

</RelativeLayout>

I Am Not The Owner Of These Code .I Merely Have Copied Them From Various Sources. The Only Thing I Did Is That I Am Going To Present Them In More Easy Way To Understand.

Comments

Popular posts from this blog

LED Blinking using 8051 Microcontroller and Keil C – AT89C51

Android Camera Example 2

Java Script to make text change text color