App que muestra los sensores de un dispositivo Android

Android App SensorManager Hardware

SensorAPP

Aplicación móvil Android con Java, desarrollada en Android Studio 4.2.1

Aplicaciones Nativas

Una de las ventajas de las aplicaciones nativas en este caso de la plataforma Android es que permite utilizar de manera más directa los recursos internos del dispositivo, en este ejemplo tenemos una manera de poder acceder a la información de los sensores de un smartpohne y las características que posee.

Esta aplicación contiene el uso de la clase

android.hardware.SensorManager
para poder mostrar un listado de los sensores que se encuentran disponibles en el dispositivo, sus características y datos de su funcionamiento.


Prueba esta app en tu smartphone

Sensor App Android
Descarga la App en Google Play

Android App Sensors details

MainActivity.java


package com.sensorapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

    public List mSensors;

    static final String NAME = "name";
    static final String TYPE = "type";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SensorManager sensor_manager = (SensorManager)
                getSystemService(Context.SENSOR_SERVICE);
        List sensors = sensor_manager.getSensorList(Sensor.TYPE_ALL);

        List> list = new ArrayList>();

        for (int i = 0; i < sensors.size(); ++i) {
            Sensor sensor = sensors.get(i);
            Map map = new HashMap<>();
            map.put(NAME, sensor.getName());
            map.put(TYPE, "Type: " + sensor.getType());
            list.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                list,
                android.R.layout.simple_list_item_2,
                new String[]{NAME, TYPE},
                new int[]{android.R.id.text1, android.R.id.text2}
        );

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        showDetails(position);

                    }
                }
        );

        mSensors = sensors;


    }

    public void showDetails(int position) {
        Intent intent = new Intent(this, DetailsActivity.class);
        intent.putExtra(TYPE, mSensors.get(position).getType());
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}





DetailsActivity.java

package com.coffee;
package com.sensorapp;

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

import java.util.ArrayList;

/**
 * Created by Vladimir Salguero on 11/05/2016.
 */
public class DetailsActivity extends Activity {
    static final String TYPE = "type";

    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);

        setContentView(R.layout.details_activity);

        int type = getIntent().getIntExtra(MainActivity.TYPE, 0);

        SensorManager sensor_manager = (SensorManager)
                getSystemService(Context.SENSOR_SERVICE);

        Sensor sensor = sensor_manager.getDefaultSensor(type);

        ArrayList list = new ArrayList<>();

        ListView listView = (ListView) findViewById(R.id.details_view);


        list.add("Resolution: " + sensor.getResolution());
        list.add("Max range: " + sensor.getMaximumRange());
        list.add("Min delay: " + sensor.getMinDelay());
        list.add("Power: " + sensor.getPower());
        list.add("Vendor: " + sensor.getVendor());
        list.add("Version: " + sensor.getVersion());

        ArrayAdapter adapter = new ArrayAdapter(
                this, android.R.layout.simple_list_item_1, list
        );

        listView.setAdapter(adapter);


    }

    public void trySensor(View view) {
        Intent intent = new Intent(this, TrySensorActivity.class);
        int type = getIntent().getIntExtra(MainActivity.TYPE, 0);
        intent.putExtra(TYPE, type);
        startActivity(intent);
    }

}



TrySensorActivity.java

package com.sensorapp;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Vladimir Salguero on 16/05/2016.
 */
public class TrySensorActivity extends Activity implements SensorEventListener {
    private SensorManager mSensorManager;
    TextView mTimeStamp;
    TextView mAccuracy;
    Sensor mSensor;
    static final String NAME = "name";
    static final String VALUE = "value";


    @Override
    public final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sensor_activity);
        mTimeStamp = (TextView) findViewById(R.id.timestamp);
        mAccuracy = (TextView) findViewById(R.id.accuracy);


        int type = getIntent().getIntExtra(DetailsActivity.TYPE, 0);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(type);

        //mostrar nombre del sensor
        TextView view = (TextView) findViewById(R.id.sensorname);
        view.setText(mSensor.getName());


    }


    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
        mAccuracy.setText("Accuracy: " + accuracy);
    }


    public final void onSensorChanged(SensorEvent event) {
        mTimeStamp.setText("Timestamp: " + String.valueOf(event.timestamp));

        float[] mValues;
        mValues = event.values;

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();

        for (int i = 0; i < mValues.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put(VALUE, "Data[" + i + "] " + mValues[i]);
            list.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                list,
                android.R.layout.simple_list_item_2,
                new String[]{NAME,VALUE},
                new int[]{android.R.id.text1, android.R.id.text2}
        );

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }


}




Layout

.../main/res/layout


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="vertical"
        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">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

        <ImageView
                android:layout_width="188dp"
                android:layout_height="132dp"
                android:gravity="center"
                android:scaleType="centerInside"
                android:src="@drawable/sensor" />

    </LinearLayout>


    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/sensor_title"
            android:textColor="#3a5465"
            android:textStyle="bold"
            android:layout_marginBottom="10dp"
            android:textSize="26dp" />


    <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:scrollbars="vertical" />

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/creator"
            android:textColor="#666666"
            android:textSize="16dp" />


</LinearLayout>




details_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:padding="16dp"
    android:orientation="vertical">

    <ListView
        android:id="@+id/details_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="trySensor"
        android:padding="16dp"
        android:text="TRY IT!" />


</LinearLayout>



sensor_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:padding="12dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/sensorname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:textSize="24dp" />

    <TextView
        android:id="@+id/accuracy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/timestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:textSize="20dp" />

     <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:scrollbars="vertical" />

</LinearLayout>


Prueba la aplicación en tu smartphone


Descarga la App en Google Play


Github Vladimir Salguero

Visita el repositorio en GitHub





Support the project, Buy me a coffee!!

buymeacoffee


0 Comentarios

Vladimir Salguero