- 概述
大多数安卓设备都内置了传感器,用来测量移动,方向和各种环境条件。
Android平台支持以下三类传感器:
这些传感器能够提供精度高且准确的原始数据。
如果要监视三维设备运动或定位,或者监视设备周围的环境变化,那么传感器会极其有用。
例如:游戏可以追踪设备重力感应器的读数来推断复杂的用户手势和动作,比如倾斜,摇晃,旋转或摆动。
同样地,天气应用程序可以使用设备温度传感器和湿度传感器来计算并报告dewpoint,旅行应用程序可以使用地磁传感器和加速计来报告罗盘方位。
运动传感器:这些传感器测量三个轴的加速力和旋转力。这一类别包括加速度计,重力感应器,陀螺仪和旋转矢量传感器。
环境传感器:这些传感器测量各种环境参数,例如环境空气温度和压力,照明和湿度。此类别包括了气压计,光度计和温度计。
位置传感器:这些传感器测量了设备的物理位置。此类别包括方向传感器和磁力计。
获取本机传感器
//获取系统传感器管理器
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//通过系统传感器管理器..获取本机所有传感器.
List<Sensor> sensorList = sm.getSensorList(Sensor.TYPE_ALL);
for(Sensor s :sensorList){
Log.i("sensorList",s.toString());
}
获取指定的某一个传感器
注册传感器的监听对象
//注册传感器的监听对象
sm.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
//传感器数据变化,在该方中我们可以获取传感器变化的值
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
textView_content.setText("x="+x+"y="+y+"z="+z);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//传感器精度变化
}
},type_acclerometer,SensorManager.SENSOR_DELAY_NORMAL);
摇一摇测试
int ringValue=40;//XYZ三值的总和表示摇晃
boolean flag=false;//是否播放声音
if ((Math.abs(x)+Math.abs(y)+Math.abs(z))>=ringValue && flag==false){
flag=true;
textView_start.setVisibility(View.VISIBLE);
MediaPlayer mp= MediaPlayer.create(MainActivity.this,R.raw.zhh);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
flag=false;
textView_start.setVisibility(View.GONE);
}
});
温度传感器
//温度传感器
Sensor type_ambient_temperatyre=sm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
sm.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float temp=event.values[0];
temp= (float) (Math.round(temp*10.0)/10.0);
textView_temp.setText("当前温度:"+temp+"℃");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
},type_ambient_temperatyre,SensorManager.SENSOR_DELAY_NORMAL);
自由的小方块
Rect
package com.example.sensor;
public class Rect {
float left=100,top=100,right=100,bottom=100;
private int width,height;
public Rect(int width,int height)
{
this.width=width;
this.height=height;
left=width/2-right/2;
top=height/2-bottom/2;
}
public float getBottom() {
return bottom;
}
public void setBottom(float bottom) {
this.bottom = bottom;
}
public float getLeft() {
return left;
}
public void setLeft(float left) {
this.left -= left;
if (this.left<=0)
{
this.left=0;
}
else if (this.left>=width-this.right)
{
this.left=width-this.right;
}
}
public float getRight() {
return right;
}
public void setRight(float right) {
this.right = right;
}
public float getTop() {
return top;
}
public void setTop(float top) {
this.top+= top;
if (this.top<=0)
{
this.top=0;
}
else if ( this.top>=height-this.bottom)
{
this.top=height-this.bottom;
}
}
}
自定义视图
RectView
package com.example.sensor;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class RectView extends View {
Rect rect;
private int width,height;
Paint paint;
public RectView(Context context,int width,int height) {
super(context);
this.width=width;
this.height=height;
paint =new Paint();
paint.setColor(Color.GREEN);
rect=new Rect(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(rect.getLeft(),rect.getTop(),rect.getRight()+rect.getLeft(),rect.getTop()+rect.getBottom(),paint);
}
}
package com.example.sensor;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
public class Main2Activity extends Activity {
private RectView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
rv = new RectView(this, dm.widthPixels, dm.heightPixels);
setContentView(rv);
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor type_gravity = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);
sm.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
rv.rect.setLeft(Math.round(x));
rv.rect.setTop(Math.round(y));
rv.invalidate();//屏幕重绘
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, type_gravity, SensorManager.SENSOR_DELAY_GAME);
}
}