欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-13 12:29:58
本文实例讲述了android编程实现获取所有传感器数据的方法。分享给大家供大家参考,具体如下: main.xml

本文实例讲述了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程序设计有所帮助。

上一篇:

下一篇: