Android下调用传感器

来源:互联网 发布:橱柜在线设计软件 编辑:程序博客网 时间:2024/06/04 18:15
 

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
01package me.sigma.sensors;
02 
03 
04importandroid.app.Activity;
05importandroid.hardware.SensorListener;
06importandroid.hardware.SensorManager;
07importandroid.os.Bundle;
08importandroid.widget.TextView;
09 
10publicclass Sensors extendsActivity {
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    SensorManager  mySensorManager;//
28    @Override  
29    publicvoid 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    privateSensorListener mySensorListener = newSensorListener(){
46        @Override
47        publicvoid onAccuracyChanged(intsensor, int accuracy) {}  
48        @Override
49        publicvoid onSensorChanged(intsensor, float[] values) {        
50            if(sensor == SensorManager.SENSOR_TEMPERATURE){
51                myTextView1.setText("Current Temprature:"+values[0]);    
52            }elseif(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            }elseif(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            }elseif(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            }elseif(sensor == SensorManager.SENSOR_LIGHT){
65                myTextView11.setText("Current Oraenttation x:"+values[0]);
66            }
67        }
68    };
69    @Override
70    protectedvoid 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    protectedvoid onPause() {
84        mySensorManager.unregisterListener(mySensorListener);
85        super.onPause();
86    }
87}  

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

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