Android下调用传感器(温度,亮度方向,地磁等,附源码)

装好Adroid SDK后,在sample的指导下,写了几个简单的类似hello world的程序,在这里介绍一下所写的在android下调用传感器的程序。

Android中支持的几种传感器:

Sensor.TYPE_ACCELEROMETER:加速度传感器。
Sensor.TYPE_GYROSCOPE:陀螺仪传感器。
Sensor.TYPE_LIGHT:亮度传感器。
Sensor.TYPE_MAGNETIC_FIELD:地磁传感器。
Sensor.TYPE_ORIENTATION:方向传感器。
Sensor.TYPE_PRESSURE:压力传感器。
Sensor.TYPE_PROXIMITY:近程传感器。
Sensor.TYPE_TEMPERATURE:温度传感器。

使用传感器最关键的一些知识是:SensorManager是所有传感器的一个综合管理类,包括了传感器的种类、采样率、精准度等。我们可以通过getSystemService方法来取得一个SensorManager对象。使用传感器时,需要通过registerListener函数注册传感器,使用完后需要通过unregisterListener取消注册。

百闻不如一见,还是直接讲代码:

新建一个Sensors的工程,创建一个Sensors.java,内容如下:

20········30········40········50········60········70········80········90········100 01 package me.sigma.sensors; 02 03 04 import android.app.Activity; 05 import android.hardware.SensorListener; 06 import android.hardware.SensorManager; 07 import android.os.Bundle; 08 import android.widget.TextView; 09 10 public class Sensors extends Activity { 11 TextView myTextView1;//t 12 //gen 13 TextView myTextView2;//x 14 TextView myTextView3;//y 15 TextView myTextView4;//z 16 //acc 17 TextView myTextView5;//x 18 TextView myTextView6;//y 19 TextView myTextView7;//z 20 //ori 21 TextView myTextView8;//x 22 TextView myTextView9;//y 23 TextView myTextView10;//z 24 //Light 25 TextView myTextView11;//z 26 27 SensorManagermySensorManager;// 28 @Override 29 public void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.main); 32 myTextView1 = (TextView) findViewById(R.id.myTextView1); 33 myTextView2 = (TextView) findViewById(R.id.myTextView2); 34 myTextView3 = (TextView) findViewById(R.id.myTextView3); 35 myTextView4 = (TextView) findViewById(R.id.myTextView4); 36 myTextView5 = (TextView) findViewById(R.id.myTextView5); 37 myTextView6 = (TextView) findViewById(R.id.myTextView6); 38 myTextView7 = (TextView) findViewById(R.id.myTextView7); 39 myTextView8 = (TextView) findViewById(R.id.myTextView8); 40 myTextView9 = (TextView) findViewById(R.id.myTextView9); 41 myTextView10 = (TextView) findViewById(R.id.myTextView10); 42 myTextView11 = (TextView) findViewById(R.id.myTextView11); 43 mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 44 } 45 private SensorListener mySensorListener = new SensorListener(){ 46 @Override 47 public void onAccuracyChanged(int sensor, int accuracy) {} 48 @Override 49 public void onSensorChanged(int sensor, float[] values) { 50 if(sensor == SensorManager.SENSOR_TEMPERATURE){ 51 myTextView1.setText("Current Temprature:"+values[0]); 52 }else if(sensor == SensorManager.SENSOR_MAGNETIC_FIELD){ 53 myTextView2.setText("Current Magnetic x:"+values[0]); 54 myTextView3.setText("Current Magnetic y:"+values[1]); 55 myTextView4.setText("Current Magnetic z:"+values[2]); 56 }else if(sensor == SensorManager.SENSOR_ACCELEROMETER){ 57 myTextView5.setText("Current Accelero x:"+values[0]); 58 myTextView6.setText("Current Accelero y:"+values[1]); 59 myTextView7.setText("Current Accelero z:"+values[2]); 60 }else if(sensor == SensorManager.SENSOR_ORIENTATION){ 61 myTextView8.setText("Current Oraenttation x:"+values[0]); 62 myTextView9.setText("Current Oraenttation y:"+values[1]); 63 myTextView10.setText("Current Oraenttation z:"+values[2]); 64 }else if(sensor == SensorManager.SENSOR_LIGHT){ 65 myTextView11.setText("Current Oraenttation x:"+values[0]); 66 } 67 } 68 }; 69 @Override 70 protected void onResume() { 71 mySensorManager.registerListener( 72 mySensorListener, 73 SensorManager.SENSOR_TEMPERATURE | 74 SensorManager.SENSOR_MAGNETIC_FIELD | 75 SensorManager.SENSOR_ACCELEROMETER | 76 SensorManager.SENSOR_LIGHT | 77 SensorManager.SENSOR_ORIENTATION, 78 SensorManager.SENSOR_DELAY_UI 79 ); 80 super.onResume(); 81 } 82 @Override 83 protected void onPause() { 84 mySensorManager.unregisterListener(mySensorListener); 85 super.onPause(); 86 } 87 }

更改res/layout/下面的main.xml,为如下内容:

01 <?xml version="1.0" encoding="utf-8"?> 02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03 android:orientation="vertical" 04 android:layout_width="fill_parent" 05 android:layout_height="fill_parent"> 06 <TextView 07 android:id="@+id/title" 08 android:gravity="center_horizontal" 09 android:textSize="20px" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="@string/title"/> 13 <TextView 14 android:id="@+id/myTextView1" 15 android:textSize="18px" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:text="@string/myTextView1"/> 19 <TextView 20 android:id="@+id/myTextView2" 21 android:textSize="18px" 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:text="@string/myTextView2"/> 25 <TextView 26 android:id="@+id/myTextView3" 27 android:textSize="18px" 28 android:layout_width="fill_parent" 29 android:layout_height="wrap_content" 30 android:text="@string/myTextView3"/> 31 <TextView 32 android:id="@+id/myTextView4" 33 android:textSize="18px" 34 android:layout_width="fill_parent" 35 android:layout_height="wrap_content" 36 android:text="@string/myTextView4"/> 37 <TextView 38 android:id="@+id/myTextView5" 39 android:textSize="18px" 40 android:layout_width="fill_parent" 41 android:layout_height="wrap_content" 42 android:text="@string/myTextView5"/> 43 <TextView 44 android:id="@+id/myTextView6" 45 android:textSize="18px" 46 android:layout_width="fill_parent" 47 android:layout_height="wrap_content" 48 android:text="@string/myTextView6"/> 49 <TextView 50 android:id="@+id/myTextView7" 51 android:textSize="18px" 52 android:layout_width="fill_parent" 53 android:layout_height="wrap_content" 54 android:text="@string/myTextView7"/> 55 <TextView 56 android:id="@+id/myTextView8" 57 android:textSize="18px" 58 android:layout_width="fill_parent" 59 android:layout_height="wrap_content" 60 android:text="@string/myTextView8"/> 61 <TextView 62 android:id="@+id/myTextView9" 63 android:textSize="18px" 64 android:layout_width="fill_parent" 65 android:layout_height="wrap_content" 66 android:text="@string/myTextView9"/> 67 <TextView 68 android:id="@+id/myTextView10" 69 android:textSize="18px" 70 android:layout_width="fill_parent" 71 android:layout_height="wrap_content" 72 android:text="@string/myTextView10"/> 73 <TextView 74 android:id="@+id/myTextView11" 75 android:textSize="18px" 76 android:layout_width="fill_parent" 77 android:layout_height="wrap_content" 78 android:text="@string/myTextView11"/> 79 </LinearLayout> 80

更改res/values/strings.xml为如下内容:

80········90········100 01 <?xml version="1.0" encoding="utf-8"?> 02 <resources> 03 <string name="hello">templator!</string> 04 <string name="app_name">templator</string> 05 <string name="title">Sigma Sensors</string> 06 <string name="myTextView1">Current Temprature:</string> 07 <string name="myTextView2">Current Magnetic x:</string> 08 <string name="myTextView3">Current Magnetic y:</string> 09 <string name="myTextView4">Current Magnetic z:</string> 10 <string name="myTextView5">Current Accelero x:</string> 11 <string name="myTextView6">Current Accelero y:</string> 12 <string name="myTextView7">Current Accelero z:</string> 13 <string name="myTextView8">Current Oraenttation x:</string> 14 <string name="myTextView9">Current Oraenttation y:</string> 15 <string name="myTextView10">Current Oraenttation z:</string> 16 <string name="myTextView11">Current Light:</string> 17 </resources> 18

更多相关文章

  1. Android Studio App设置TextView文字内容大小颜色
  2. 值得学习的博客内容
  3. Android硬件之传感器
  4. android中根据控件宽度,实现展示文本内容,解决中英文自动换行
  5. Android Sensor传感器系统架构初探

随机推荐

  1. Android(安卓)Studio试用总结
  2. [置顶] Android入门系列一(Android学习方
  3. Android(安卓)Adapter详解(1)
  4. Android(安卓)事件处理
  5. Android(安卓)AsyncTask解析
  6. android日志分析与记录.
  7. 理解Android的菜单
  8. Android高手进阶教程(四)之----Android(
  9. .net程序员转战android第二篇---牛刀小试
  10. Android(安卓)NFS文件系统挂载遇到的问题