本文实例讲述了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
# 获取
# 传感器
# 数据
# android 传感器(OnSensorChanged)使用介绍
# Android利用方向传感器获得手机的相对角度实例说明
# Android 利用方向传感器实现指南针具体步骤
# Android利用Sensor(传感器)实现指南针小功能
# Android开发中的重力传感器用法实例详解
# Android利用Sensor(传感器)实现水平仪功能
# Android实现电子罗盘(指南针)方向传感器的应用
# Android开发中方向传感器定义与用法详解【附指南针实现方法】
# Android实现计步传感器功能
# Android传感器的简单使用方法
# 陀螺仪
# 操作技巧
# 进阶
# 相关内容
# 感兴趣
# 给大家
# 更多关于
# 所述
# 程序设计
# 讲述了
# java
# defalut
# code
# IBM
# IBMEyes
# sample
# unregisterListener
# import
# app
相关文章:
建站之星后台密码如何安全设置与找回?
太原网站制作公司有哪些,网约车营运证查询官网?
南阳网站制作公司推荐,小学电子版试卷去哪里找资源好?
建站之星体验版:智能建站系统+响应式设计,多端适配快速建站
内网网站制作软件,内网的网站如何发布到外网?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
建站主机是否等同于虚拟主机?
宿州网站制作公司兴策,安徽省低保查询网站?
如何快速启动建站代理加盟业务?
网站图片在线制作软件,怎么在图片上做链接?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
网站好制作吗知乎,网站开发好学吗?有什么技巧?
昆明网站制作哪家好,昆明公租房申请网上登录入口?
上海网站制作开发公司,上海买房比较好的网站有哪些?
如何在阿里云完成域名注册与建站?
如何在腾讯云免费申请建站?
C#怎么使用委托和事件 C# delegate与event编程方法
如何通过老薛主机一键快速建站?
临沂网站制作企业,临沂第三中学官方网站?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
,南京靠谱的征婚网站?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
如何在万网开始建站?分步指南解析
如何在阿里云虚拟主机上快速搭建个人网站?
百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?
建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析
如何设计高效校园网站?
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
山东网站制作公司有哪些,山东大源集团官网?
制作网站的公司有哪些,做一个公司网站要多少钱?
宝塔Windows建站如何避免显示默认IIS页面?
平台云上自助建站如何快速打造专业网站?
建站之家VIP精选网站模板与SEO优化教程整合指南
可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?
北京制作网站的公司,北京铁路集团官方网站?
如何快速搭建虚拟主机网站?新手必看指南
公司网站制作费用多少,为公司建立一个网站需要哪些费用?
c++ stringstream用法详解_c++字符串与数字转换利器
专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?
网站制作知乎推荐,想做自己的网站用什么工具比较好?
h5在线制作网站电脑版下载,h5网页制作软件?
武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?
建站之星如何取消后台验证码生成?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
IOS倒计时设置UIButton标题title的抖动问题
如何选择网络建站服务器?高效建站必看指南
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
如何通过商城免费建站系统源码自定义网站主题?
模具网站制作流程,如何找模具客户?
杭州银行网站设计制作流程,杭州银行怎么开通认证方式?
*请认真填写需求信息,我们会在24小时内与您取得联系。