Android编程实现获取所有传感器数据的方法

 更新时间:2017年06月01日 11:57:03   作者:jxgxy  
这篇文章主要介绍了Android编程实现获取所有传感器数据的方法,涉及Android针对传感器Sensor相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android编程实现获取所有传感器数据的方法。分享给大家供大家参考,具体如下:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<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相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • 实例讲解Android中ContentProvider组件的使用方法

    实例讲解Android中ContentProvider组件的使用方法

    这篇文章主要介绍了Android中ContentProvider组件的使用方法,包括ContentProvider使用单元测试的步骤,需要的朋友可以参考下
    2016-04-04
  • Android12四大组件之Activity生命周期变化详解

    Android12四大组件之Activity生命周期变化详解

    虽然说我们天天都在使用Activity,但是你真的对Activity的生命机制完全了解了吗?Activity的生命周期方法只有七个,但是其实那只是默认的情况。也就是说在其他情况下,Activity的生命周期可能不会是按照我们以前所知道的流程,本章着重讲解Activity的生命周期变化
    2022-07-07
  • Android 开发中线程的分析

    Android 开发中线程的分析

    这篇文章主要介绍了Android 开发中线程的分析的相关资料,需要的朋友可以参考下
    2017-04-04
  • android调用H5显示加载中效果的示例代码

    android调用H5显示加载中效果的示例代码

    本篇文章主要介绍了android调用H5显示加载中效果的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android从网络中获得一张图片并显示在屏幕上的实例详解

    Android从网络中获得一张图片并显示在屏幕上的实例详解

    这篇文章主要介绍了Android从网络中获得一张图片并显示在屏幕上的实例详解的相关资料,希望通过本文能帮助大家实现这样的功能,需要的朋友可以参考下
    2017-08-08
  • Android 开发中Volley详解及实例

    Android 开发中Volley详解及实例

    这篇文章主要介绍了Android 开发中Volley详解及实例的相关资料,需要的朋友可以参考下
    2017-04-04
  • Flutter DateTime日期转换的详细使用

    Flutter DateTime日期转换的详细使用

    本文主要介绍了Flutter DateTime日期转换的详细使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Android之淘宝商品列表长按遮罩效果的实现

    Android之淘宝商品列表长按遮罩效果的实现

    这篇文章主要介绍了Android之淘宝商品列表长按遮罩效果的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android  SharedPreferences四种操作模式使用详解

    Android SharedPreferences四种操作模式使用详解

    这篇文章主要介绍了Android SharedPreferences四种操作模式使用详解的相关资料,这里介绍了获取Android SharedPreferences的两种方法及比较,和操作模式的介绍,需要的朋友可以参考下
    2017-07-07
  • android设备间实现无线投屏的示例代码

    android设备间实现无线投屏的示例代码

    Android提供了MediaProjection来实现录屏,通过MediaProjection可以获取当前屏幕的视频流,而视频流需要通过编解码来压缩进行传输,通过MediaCodec可实现视频的编码和解码,这篇文章主要介绍了android设备间实现无线投屏,需要的朋友可以参考下
    2022-06-06

最新评论