Android传感器的使用(GravieySensor)

这里以重力传感器为例说明

第一步:创建一个SensorManager对象,用来管理或者获取传感器

SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);

第二步:根据Sensor下枚举获得对应的传感器对象

 Sensor sr = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);

第三步,为该传感器注册一个事件,方法有很多种,可以让Activi继承SensorEventListener接口,或者直接使用内部类,推荐使用内部类:

 sm.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {

            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        }, sr, SensorManager.SENSOR_DELAY_NORMAL);

其中,第三个参数,SENSOR_DELAY_NORMAL表示传感器反应速度,这里SENSOR_DELAY_GAME,SENSOR_DELAY_FAST等。

第四步:SensorEvent对象中的values[]数组中保存了传感器具体数值,针对不同的传感器对象,values[]具有不同内容,长度也不一样。对于Gravity传感器,这里values有三个值,分别代表了其在x,y,z方向上的重力加速度,我们在xml文件中简单设置一下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_gravity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xdwang.myapplication.Gravity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/x"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/y"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/z"/>
</LinearLayout>

然后在onSensorChanged中对三个TXTVIEW设置值:

 sm.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                txtx.setText("x方向的重力加速度为:" + event.values[0] + "g/ms2");
                txty.setText("y方向的重力加速度为:" + event.values[1] + "g/ms2");
                txtz.setText("z方向的重力加速度为:" + event.values[2] + "g/ms2");
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        }, sr, SensorManager.SENSOR_DELAY_NORMAL);

这样,我们就能实时得到重力传感器的数据了,具体怎么使用这些数据,就要看你的用处了。

最后附上全部代码:

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Gravity extends AppCompatActivity {
    TextView txtx = null;
    TextView txty = null;
    TextView txtz = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gravity);
        SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
        Sensor sr = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);
        txtx = (TextView) findViewById(R.id.x);
        txty = (TextView) findViewById(R.id.y);
        txtz = (TextView) findViewById(R.id.z);
        sm.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                txtx.setText("x方向的重力加速度为:" + event.values[0] + "g/ms2");
                txty.setText("y方向的重力加速度为:" + event.values[1] + "g/ms2");
                txtz.setText("z方向的重力加速度为:" + event.values[2] + "g/ms2");
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        }, sr, SensorManager.SENSOR_DELAY_NORMAL);
    }
}

 

posted @ 2016-10-08 23:52  啊王会  阅读(2198)  评论(0编辑  收藏  举报