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

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

时间: 2024-08-30 10:58:32

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

Android编程简单解析JSON格式数据的方法示例

本文实例讲述了Android编程简单解析JSON格式数据的方法.分享给大家供大家参考,具体如下: 比起XML,JSON主要优势在于它的体积更小,在网络上传输的时候可以更省流量.但缺点在于,它的语义性较差,显示不如XML直观. JSON格式 : { "name_A" : "value_A","name_B" : "value_B" } 表示: name_A = value_A; name_B = value_B; 我将对下面的J

Android编程实现获取图片资源的四种方法_Android

本文实例讲述了Android编程实现获取图片资源的四种方法.分享给大家供大家参考,具体如下: 1. 图片放在sdcard中: 复制代码 代码如下: Bitmap imageBitmap = BitmapFactory.decodeFile(path)//path 是图片的路径,跟目录是/sdcard   2. 图片在项目的res文件夹下面 //得到application对象 ApplicationInfo appInfo = getApplicationInfo(); //得到该图片的id(na

Android编程使用sax解析xml数据的方法详解

本文实例讲述了Android编程使用sax解析xml数据的方法.分享给大家供大家参考,具体如下: 随着技术的发展,现在的web已经和以前不同了.web已经逐渐像移动的方向倾斜,作为程序员的确应该拓展一下自己的知识层面.学习各方面的知识,今天就接着前几天的弄一下Android的xml解析,这次就使用sax的方式解析xml.下面就一步一步的来做吧. 1. 编写一个简单的xml <?xml version="1.0" encoding="UTF-8"?> &l

Android编程实现获取图片资源的四种方法

本文实例讲述了Android编程实现获取图片资源的四种方法.分享给大家供大家参考,具体如下: 1. 图片放在sdcard中: 复制代码 代码如下:Bitmap imageBitmap = BitmapFactory.decodeFile(path)//path 是图片的路径,跟目录是/sdcard   2. 图片在项目的res文件夹下面 //得到application对象 ApplicationInfo appInfo = getApplicationInfo(); //得到该图片的id(nam

Android编程实现获取新浪天气预报数据的方法_Android

本文实例讲述了Android编程实现获取新浪天气预报数据的方法.分享给大家供大家参考,具体如下: 新浪天气预报地址: http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&day=0 其中,city后的城市可用java.net.URLEncoder.encode("武汉"," gb2312");也可以直接写"武汉",但不能用"wu

Android开发获取传感器数据的方法示例【加速度传感器,磁场传感器,光线传感器,方向传感器】

本文实例讲述了Android开发获取传感器数据的方法.分享给大家供大家参考,具体如下: package mobile.android.sensor; import java.util.List; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import an

Android编程实现获取新浪天气预报数据的方法

本文实例讲述了Android编程实现获取新浪天气预报数据的方法.分享给大家供大家参考,具体如下: 新浪天气预报地址: http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&day=0 其中,city后的城市可用java.net.URLEncoder.encode("武汉"," gb2312");也可以直接写"武汉",但不能用"wu

Android编程实现获取系统内存、CPU使用率及状态栏高度的方法示例

本文实例讲述了Android编程实现获取系统内存.CPU使用率及状态栏高度的方法.分享给大家供大家参考,具体如下: DeviceInfoManage类用于获取系统的内存,CPU的信息,以及状态栏的高度 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReade

服务器-关于java网络编程中获取输入流中数据的问题?

问题描述 关于java网络编程中获取输入流中数据的问题? //服务器端接收消息的类.定制端口号为8888 serviceSocket = new ServerSocket(10000); //获取socket.这个方法是阻塞式的 socket = serviceSocket.accept(); inputStream = socket.getInputStream(); byte buf[] = new byte[1024]; int len = 0; len =inputStream.read