原文链接: https://www.cnblogs.com/wzxwhd/p/5940665.html

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

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

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

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

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

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

按 Ctrl+C 复制代码 按 Ctrl+C 复制代码

其中,第三个参数,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;

@Overrideprotected 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);}

}

 

更多相关文章

  1. android 强制设置横屏 判断是横屏还是竖屏
  2. no drawer view found with gravity RIGHT(Android实现抽屉从右
  3. Android使用libgdx实现模拟方向键控制角色移动的方法
  4. 2.2.1 LinearLayout(线性布局)
  5. Android传感器(五):线性加速度传感器
  6. Android传感器学习总结
  7. android中重写onConfigurationChanged方法响应系统设置更改
  8. android 加速的传感器(重力传感器)
  9. 20180505_android传感器种类及获取

随机推荐

  1. Android 进度条功能实现
  2. Android 使用SVG
  3. Android(安卓)LottieAnimation使用---踩
  4. Android 学习日志 2 :创建虚拟机,运行第一
  5. Android反编译工具的使用-Android(安卓)K
  6. Android热补丁方案对比
  7. Android 中需要掌握的高级技巧
  8. [置顶] android四大组件之intent
  9. 在android里调用第三方动态链接库.so
  10. Android(安卓)网络调试