温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中实现如何获取所有传感器数据的

发布时间:2020-12-07 15:27:56 来源:亿速云 阅读:124 作者:Leah 栏目:移动开发

这篇文章给大家介绍Android应用中实现如何获取所有传感器数据的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="加速度"
  android:id="@+id/edt1"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="磁场"
  android:id="@+id/edt2"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="定位"
  android:id="@+id/edt3"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="陀螺仪"
  android:id="@+id/edt4"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="光线"
  android:id="@+id/edt5"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="压力"
  android:id="@+id/edt6"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="温度"
  android:id="@+id/edt7"
  />
    <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="距离"
  android:id="@+id/edt8"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="重力"
  android:id="@+id/edt9"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="线性加速度"
  android:id="@+id/edt10"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="旋转矢量"
  android:id="@+id/edt11"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="defalut"
  android:id="@+id/edt12"
  />
</LinearLayout>

main.java

/*
 *
 * IBMEyes.java
 * sample code for IBM Developerworks Article
 * Author: W. Frank Ableson
 * fableson@msiservices.com
 *
 */
package com.msi.ibm.eyes;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorListener;
public class IBMEyes extends Activity implements SensorListener {
  final String tag = "IBMEyes";
  SensorManager sm = null;
  TextView View1 = null;
  TextView View2 = null;
  TextView View3 = null;
  TextView View4 = null;
  TextView View5 = null;
  TextView View6 = null;
  TextView View7 = null;
  TextView View8 = null;
  TextView View9 = null;
  TextView View10 = null;
  TextView View11 = null;
  TextView View12 = null;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    setContentView(R.layout.main);
    View1 = (TextView) findViewById(R.id.edt1);
    View2 = (TextView) findViewById(R.id.edt2);
    View3 = (TextView) findViewById(R.id.edt3);
    View4 = (TextView) findViewById(R.id.edt4);
    View5 = (TextView) findViewById(R.id.edt5);
    View6 = (TextView) findViewById(R.id.edt6);
    View7 = (TextView) findViewById(R.id.edt7);
    View8 = (TextView) findViewById(R.id.edt8);
    View9 = (TextView) findViewById(R.id.edt9);
    View10 = (TextView) findViewById(R.id.edt10);
    View11 = (TextView) findViewById(R.id.edt11);
    View12 = (TextView) findViewById(R.id.edt12);
  }
  public void onSensorChanged(int sensor, float[] values) {
    synchronized (this) {
      String str = "X:" + values[0] + ",Y:" + values[1] + ",Z:" + values[2];
      switch (sensor){
      case Sensor.TYPE_ACCELEROMETER:
        View1.setText("加速度:" + str);
        break;
      case Sensor.TYPE_MAGNETIC_FIELD:
        View2.setText("磁场:" + str);
        break;
      case Sensor.TYPE_ORIENTATION:
        View3.setText("定位:" + str);
        break;
      case Sensor.TYPE_GYROSCOPE:
        View4.setText("陀螺仪:" + str);
        break;
      case Sensor.TYPE_LIGHT:
        View5.setText("光线:" + str);
        break;
      case Sensor.TYPE_PRESSURE:
        View6.setText("压力:" + str);
        break;
      case Sensor.TYPE_TEMPERATURE:
        View7.setText("温度:" + str);
        break;
      case Sensor.TYPE_PROXIMITY:
        View8.setText("距离:" + str);
        break;
      case Sensor.TYPE_GRAVITY:
        View9.setText("重力:" + str);
        break;
      case Sensor.TYPE_LINEAR_ACCELERATION:
        View10.setText("线性加速度:" + str);
        break;
      case Sensor.TYPE_ROTATION_VECTOR:
        View11.setText("旋转矢量:" + str);
        break;
      default:
        View12.setText("NORMAL:" + str);
        break;
      }
    }
  }
  public void onAccuracyChanged(int sensor, int accuracy) {
    Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
  }
  @Override
  protected void onResume() {
    super.onResume();
    sm.registerListener(this,
        Sensor.TYPE_ACCELEROMETER |
        Sensor.TYPE_MAGNETIC_FIELD |
        Sensor.TYPE_ORIENTATION |
        Sensor.TYPE_GYROSCOPE |
        Sensor.TYPE_LIGHT |
        Sensor.TYPE_PRESSURE |
        Sensor.TYPE_TEMPERATURE |
        Sensor.TYPE_PROXIMITY |
        Sensor.TYPE_GRAVITY |
        Sensor.TYPE_LINEAR_ACCELERATION |
        Sensor.TYPE_ROTATION_VECTOR,
        SensorManager.SENSOR_DELAY_NORMAL);
  }
  @Override
  protected void onStop() {
    sm.unregisterListener(this);
    super.onStop();
  }
}

关于Android应用中实现如何获取所有传感器数据的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI