android 测量温度传感器,[转载]android 硬件 传感器 温度传感器的代码例子 方向传感器代码例子 全...

本文介绍了Android中多种类型的传感器,如加速度、陀螺仪、亮度、地磁、方向、压力、近程和温度传感器。重点讲解了如何使用SensorManager获取传感器服务,并通过registerListener和unregisterListener进行监听与取消监听。示例代码展示了如何在活动中实时显示传感器数据,涉及UI更新和传感器事件处理。
摘要由CSDN通过智能技术生成

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,内容如下:

package me.sigma.sensors;

import android.app.Activity;

import android.hardware.SensorListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;

public class Sensors extends Activity {

TextView myTextView1;//t

//gen

TextView myTextView2;//x

TextView myTextView3;//y

TextView myTextView4;//z

//acc

TextView myTextView5;//x

TextView myTextView6;//y

TextView myTextView7;//z

//ori

TextView myTextView8;//x

TextView myTextView9;//y

TextView myTextView10;//z

//Light

TextView myTextView11;//z

SensorManager mySensorManager;//

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myTextView1 = (TextView) findViewById(R.id.myTextView1);

myTextView2 = (TextView) findViewById(R.id.myTextView2);

myTextView3 = (TextView) findViewById(R.id.myTextView3);

myTextView4 = (TextView) findViewById(R.id.myTextView4);

myTextView5 = (TextView) findViewById(R.id.myTextView5);

myTextView6 = (TextView) findViewById(R.id.myTextView6);

myTextView7 = (TextView) findViewById(R.id.myTextView7);

myTextView8 = (TextView) findViewById(R.id.myTextView8);

myTextView9 = (TextView) findViewById(R.id.myTextView9);

myTextView10 = (TextView) findViewById(R.id.myTextView10);

myTextView11 = (TextView) findViewById(R.id.myTextView11);

mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

}

private SensorListener mySensorListener = new SensorListener(){

@Override

public void onAccuracyChanged(int sensor, int accuracy) {}

@Override

public void onSensorChanged(int sensor, float[] values) {

if(sensor == SensorManager.SENSOR_TEMPERATURE){

myTextView1.setText("Current Temprature:"+values[0]);

}else if(sensor == SensorManager.SENSOR_MAGNETIC_FIELD){

myTextView2.setText("Current Magnetic x:"+values[0]);

myTextView3.setText("Current Magnetic y:"+values[1]);

myTextView4.setText("Current Magnetic z:"+values[2]);

}else if(sensor == SensorManager.SENSOR_ACCELEROMETER){

myTextView5.setText("Current Accelero x:"+values[0]);

myTextView6.setText("Current Accelero y:"+values[1]);

myTextView7.setText("Current Accelero z:"+values[2]);

}else if(sensor == SensorManager.SENSOR_ORIENTATION){

myTextView8.setText("Current Oraenttation x:"+values[0]);

myTextView9.setText("Current Oraenttation y:"+values[1]);

myTextView10.setText("Current Oraenttation z:"+values[2]);

}else if(sensor == SensorManager.SENSOR_LIGHT){

myTextView11.setText("Current Oraenttation x:"+values[0]);

}

}

};

@Override

protected void onResume() {

mySensorManager.registerListener(

mySensorListener,

SensorManager.SENSOR_TEMPERATURE |

SensorManager.SENSOR_MAGNETIC_FIELD |

SensorManager.SENSOR_ACCELEROMETER |

SensorManager.SENSOR_LIGHT |

SensorManager.SENSOR_ORIENTATION,

SensorManager.SENSOR_DELAY_UI

);

super.onResume();

}

@Override

protected void onPause() {

mySensorManager.unregisterListener(mySensorListener);

super.onPause();

}

}

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/title"

android:gravity="center_horizontal"

android:textSize="20px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/title"/>

android:id="@+id/myTextView1"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView1"/>

android:id="@+id/myTextView2"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView2"/>

android:id="@+id/myTextView3"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView3"/>

android:id="@+id/myTextView4"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView4"/>

android:id="@+id/myTextView5"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView5"/>

android:id="@+id/myTextView6"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView6"/>

android:id="@+id/myTextView7"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView7"/>

android:id="@+id/myTextView8"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView8"/>

android:id="@+id/myTextView9"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView9"/>

android:id="@+id/myTextView10"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView10"/>

android:id="@+id/myTextView11"

android:textSize="18px"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/myTextView11"/>

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

templator!

templator

Sigma Sensors

Current Temprature:

Current Magnetic x:

Current Magnetic y:

Current Magnetic z:

Current Accelero x:

Current Accelero y:

Current Accelero z:

Current Oraenttation x:

Current Oraenttation y:

Current Oraenttation z:

Current Light:

PS: 其实就弄那个一个mySensorManager就行 ,然后注册时候让他同时监听**1|**2|这样你在onchange里面再来个if else 就行 。这东西是精髓啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值