[Basics of Android development] Obtaining mobile phone sensor information

I. Introduction

  • Description: Regarding the use of sensors, I posted an article about the use of sensors (direction sensors, acceleration sensors) in the same column. This blog mainly focuses on obtaining sensor information supported by different mobile phones, and specifically how to use these sensors. If you need to check and study by yourself, you can also chat with me privately.
  • Blog: Sensors (direction sensor, acceleration sensor): http://t.csdn.cn/mLstV
  • Difficulty: Beginner
  • Knowledge point: Sensors
  • Effect
    Insert image description here

2. Understand

1 Overview

(First, take a look at the official website documentation)
Insert image description here
(General meaning)

       Most Android devices have built-in sensors that can measure motion, orientation, and various environmental conditions. These sensors are capable of providing high precision and accuracy when you want to monitor device movement or positioning in three dimensions, or when you want to monitor changes in the surrounding environment near the device. For example, a game might track readings from a device's gravity sensor to infer complex user gestures and movements, such as tilting, shaking, rotating, or swinging. Likewise, a weather app might use a device's temperature sensor and humidity sensor to calculate and report dew point, or a travel app might use a geomagnetic field sensor and accelerometer to report on a compass bearing.

The Android platform supports three major categories of sensors:

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

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

Position Sensors
These sensors measure the physical location of the device. This category includes orientation sensors and magnetometers.

2. Key

If you want to traverse all sensors, then this is the key

Insert image description here

List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);

3. Design

If you just want to traverse sensor information, a BaseAdapter adapter will suffice.

1.UI design

(1) Main interface
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/bin_sensor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="获取当前设备支持的传感器"/>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
(2) Adapter item
<?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">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:textSize="18dp"/>
    
</LinearLayout>

2. Encoding

(1) Obtain data (sensor information)
	private Button sensor;
    private SensorManager sm;
    private ListView list;
    private String[] adapterData;
    
    private void init() {
    
    
        list = findViewById(R.id.list);

        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = findViewById(R.id.bin_sensor);
        sensor.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
                adapterData = new String[sensors.size()];
                for (int i = 0;i < sensors.size();i++) {
    
    
                    adapterData[i] = sensors.get(i).getName();
                }
                // 初始化适配器
                initAdapter();
            }
        });
    }
(2) Rendering data (initializing the adapter)
    static class AppView{
    
    
        TextView name;
    }

    private void initAdapter() {
    
    
        BaseAdapter baseAdapter = new BaseAdapter() {
    
    
            @Override
            public int getCount() {
    
    
                return adapterData.length;
            }

            @Override
            public Object getItem(int position) {
    
    
                return adapterData[position];
            }

            @Override
            public long getItemId(int position) {
    
    
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
    
                AppView appView = null;
                if (convertView == null) {
    
    
                    convertView = View.inflate(MainActivity.this,R.layout.layout_item,null);
                    appView = new AppView();
                    appView.name = convertView.findViewById(R.id.name);
                    convertView.setTag(appView);
                } else {
    
    
                    appView = (AppView) convertView.getTag();
                }
                appView.name.setText(adapterData[position]);
                return convertView;
            }
        };
        list.setAdapter(baseAdapter);
    }

Guess you like

Origin blog.csdn.net/weixin_48916759/article/details/131291653