Monday, April 11, 2016

Sensor Part-I : Check Sensor Available on your Device

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example to check sensor available on your device with its version.

Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
***** check on physical device


A. Layout file nothing special add only one text view to show results like

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

<ScrollView 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="com.exam.ravi.sensorex3.MainActivity">

    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"       
        android:id="@+id/textView2"/>
</ScrollView>

B. Java File MainActivity.java is 

package com.exam.ravi.sensorex3;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView textView = null;
    private SensorManager sm;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView2);
        textView.setVisibility(View.GONE);

        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> sensorList = sm.getSensorList(Sensor.TYPE_ALL);

        for (int i = 1; i < sensorList.size(); i++) {
            textView.setVisibility(View.VISIBLE);
            textView.append("\n" + sensorList.get(i).getName()  + 
             "\n" + sensorList.get(i).getVersion());
        }
    }
}

No comments:

Post a Comment