zoukankan      html  css  js  c++  java
  • Android_(传感器)获取手机中的传感器

    传感器是一种检测装置,能够感受被测量的信息,并能将检测和感受到的信息按一定规律变换成电信号或其它所需形式的信息输出

    Android操作系统中内置了很多的传感器(物理装置),能够探测、感受外界的信号、物理条件,并将得到的信息传递给其它的装置。

    例如在部分游戏或软件可以自动识别屏幕的横竖屏来改变屏幕显示的布局

    下面是Android支持的几种传感器:

    加速传感器       Sensor.TYPE_ACCELEROMETER
    陀螺仪传感器      Sensor.TYPE_GYROSCOPE
    环境光仪传感器     Sensor.TYPE_LIGHT
    电磁场传感器      Sensor.TYPE_MAGNETIC_FIELD
    方向传感器       Sensor.TYPE_ORIENTATION:
    压力传感器       Sensor.TYPE_PRESSURE:
    距离传感器       Sensor.TYPE_PROXIMITY:
    温度传感器       Sensor.TYPE_TEMPERATURE:
     
     

    运行截图:

    程序结构:

     
    <?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:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context="com.example.asus.gary_01.MainActivity">
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="传感器操作!"
            android:textSize="10pt" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="获取手机传感器信息"/>
    
        <TextView
            android:id="@+id/textView"
            android:scrollbars="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:textSize="8pt"/>
    </LinearLayout>
    activity_main


    package com.example.asus.gary_01;
    
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.method.ScrollingMovementMethod;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private TextView tx1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button bt1 = (Button)findViewById(R.id.button);
            tx1=(TextView)findViewById(R.id.textView);
            //从系统获得传感器管理器
            final SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
            bt1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String str;
                    //从传感器管理器中获得全部的传感器列表
                    List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
                    int i;
                    //给ViewText添加滚动条
                    tx1.setMovementMethod(ScrollingMovementMethod.getInstance());
                    //显示有多少个传感器
                    tx1.setText("经检测该手机有"+allSensors.size()+"个传感器,它们分别是:");
                    Sensor s;
                    //显示每个传感器的具体信息
                    for(i=0;i<allSensors.size();i++)
                    {
                        s=allSensors.get(i);
                        str="设备名称:"+s.getName();
                        switch(s.getType())
                        {
                            //加速传感器     Sensor.TYPE_ACCELEROMETER
                            case Sensor.TYPE_ACCELEROMETER:
                                tx1.setText(tx1.getText()+"
    "+i+"加速传感器accelerometer:
    "+str);
                                break;
                            //陀螺仪传感器    Sensor.TYPE_GYROSCOPE
                            case Sensor.TYPE_GYROSCOPE:
                                tx1.setText(tx1.getText()+"
    "+i+"陀螺仪传感器gyroscope:
    "+str);
                                break;
                            //环境光仪传感器   Sensor.TYPE_LIGHT
                            case Sensor.TYPE_LIGHT:
                                tx1.setText(tx1.getText()+"
    "+i+"环境光仪传感器light:
    "+str);
                                break;
                            //电磁场传感器    Sensor.TYPE_MAGNETIC_FIELD
                            case Sensor.TYPE_MAGNETIC_FIELD:
                                tx1.setText(tx1.getText()+"
    "+i+"电磁场传感器magnetic:
    "+str);
                                break;
                            //方向传感器    Sensor.TYPE_ORIENTATION:
                            case Sensor.TYPE_ORIENTATION:
                                tx1.setText(tx1.getText()+"
    "+i+"方向传感器orientation:
    "+str);
                                break;
                            //压力传感器     Sensor.TYPE_PRESSURE:
                            case Sensor.TYPE_PRESSURE:
                                tx1.setText(tx1.getText()+"
    "+i+"压力传感器pressure:
    "+str);
                                break;
                            //距离传感器     Sensor.TYPE_PROXIMITY:
                            case Sensor.TYPE_PROXIMITY:
                                tx1.setText(tx1.getText()+"
    "+i+"距离传感器proximity:
    "+str);
                                break;
                            //温度传感器     Sensor.TYPE_TEMPERATURE:
                            case Sensor.TYPE_TEMPERATURE:
                                tx1.setText(tx1.getText()+"
    "+i+"温度传感器temperature:
    "+str);
                                break;
    
                                default:
                                    tx1.setText(tx1.getText()+"
    "+i+"未知传感器:
    "+str);
                                    break;
                        }
                    }
                }
            });
        }
    }
    MainActivity

    一、界面布局

      两个TextView,一个Button,下方的TextView支持滚动操作

      点击Button,获得手机传感器并显示在下方TextView上

      给textview添加滚动条:传送门

      xml代码:

      //设置滚动条的方向

      android:scrollbars="vertical"

      java中设置

      tx1=(TextView) findViewById(R.id.tv1);

      //设置滚动方式
      tx1.setMovementMethod(ScrollingMovementMethod.getInstance());

    二、实现程序

    //从系统获得传感器管理器
    final SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

    //从传感器管理器中获得全部的传感器列表
    List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);

    bt1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String str;
                    //从传感器管理器中获得全部的传感器列表
                    List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
                    int i;
                    //给ViewText添加滚动条
                    tx1.setMovementMethod(ScrollingMovementMethod.getInstance());
                    //显示有多少个传感器
                    tx1.setText("经检测该手机有"+allSensors.size()+"个传感器,它们分别是:");
                    Sensor s;
                    //显示每个传感器的具体信息
                    for(i=0;i<allSensors.size();i++)
                    {
                        s=allSensors.get(i);
                        str="设备名称:"+s.getName();
                        switch(s.getType())
                        {
                            //加速传感器     Sensor.TYPE_ACCELEROMETER
                            case Sensor.TYPE_ACCELEROMETER:
                                tx1.setText(tx1.getText()+"
    "+i+"加速传感器accelerometer:
    "+str);
                                break;
                            //陀螺仪传感器    Sensor.TYPE_GYROSCOPE
                            case Sensor.TYPE_GYROSCOPE:
                                tx1.setText(tx1.getText()+"
    "+i+"陀螺仪传感器gyroscope:
    "+str);
                                break;
                            //环境光仪传感器   Sensor.TYPE_LIGHT
                            case Sensor.TYPE_LIGHT:
                                tx1.setText(tx1.getText()+"
    "+i+"环境光仪传感器light:
    "+str);
                                break;
                            //电磁场传感器    Sensor.TYPE_MAGNETIC_FIELD
                            case Sensor.TYPE_MAGNETIC_FIELD:
                                tx1.setText(tx1.getText()+"
    "+i+"电磁场传感器magnetic:
    "+str);
                                break;
                            //方向传感器    Sensor.TYPE_ORIENTATION:
                            case Sensor.TYPE_ORIENTATION:
                                tx1.setText(tx1.getText()+"
    "+i+"方向传感器orientation:
    "+str);
                                break;
                            //压力传感器     Sensor.TYPE_PRESSURE:
                            case Sensor.TYPE_PRESSURE:
                                tx1.setText(tx1.getText()+"
    "+i+"压力传感器pressure:
    "+str);
                                break;
                            //距离传感器     Sensor.TYPE_PROXIMITY:
                            case Sensor.TYPE_PROXIMITY:
                                tx1.setText(tx1.getText()+"
    "+i+"距离传感器proximity:
    "+str);
                                break;
                            //温度传感器     Sensor.TYPE_TEMPERATURE:
                            case Sensor.TYPE_TEMPERATURE:
                                tx1.setText(tx1.getText()+"
    "+i+"温度传感器temperature:
    "+str);
                                break;
    
                                default:
                                    tx1.setText(tx1.getText()+"
    "+i+"未知传感器:
    "+str);
                                    break;
                        }
                    }
                }
            });
     
    (如需转载学习,请标明出处)
  • 相关阅读:
    小白的Python之路_day1
    Vue---tab切换时不刷新
    Vue---第十五章使用VScode编写第一个脚本
    Vue---第十四章visual studio code使用
    Vue---第十三章基础运算和v-model
    Vue---第十二章批量下载模块
    vue---第十一章全局依赖环境区分
    Vue---第十章全局变量
    Vue---第九章NPM
    hadoop-Rpc使用实例
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/9279841.html
Copyright © 2011-2022 走看看