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

第一步:创建一个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);
}
}

Android传感器的使用(GravieySensor)的更多相关文章

  1. android 传感器应用

    本章讲述Android开发中,传感器应用相关的知识点. 1.功能需求 做一个基于传感器的水平尺应用. 2.软件实现

  2. Android 传感器

    今天介绍一下Android的传感器,开发Android传感器的步骤: 1.调用Context的getSystemService(Context.SENSOR_SERVICE)方法获取SensorMan ...

  3. 《IT蓝豹》吹雪花demo,学习android传感器

    吹雪花demo,学习android传感器 吹雪花demo,学习android传感器,嘴巴对着手机底部吹一下就会出现飘着雪花效果. 算是学习android传感器效果.本例子主要是通过android.me ...

  4. 《android传感器高级编程》译者序

    翻看手机中的应用,就能发现大多数应用都已经使用了传感器.让微信彻底火起来的“附近的人”和“摇一摇”.碰一碰交换信息的Bump.各种运动记录app.神奇的“磁力探测仪”.火爆的游戏Temple Run… ...

  5. Android传感器

    Android传感器 开发传感器应用 1. 获取传感器管理者对象 // 获取传感器管理者对象 SensorManager mSensorManager = (SensorManager) getSys ...

  6. 《Android传感器高级编程》

    <Android传感器高级编程> 基本信息 原书名:Professional Android Sensor Programming 原出版社: Wrox 作者: (美)米内特(Greg M ...

  7. android传感器使用

    android传感器的使用以加速度传感器.方向传感器.磁场.压力.温度.光感.特别的距离传感器为例介绍. 首先所在的类需要implement SensorEventListener.使用传感器分为以下 ...

  8. Android传感器编程带实例

    看了程序人生 网站的 编程高手的编程感悟 深有感触,好像也是一个android 程序员写的,推荐大家也看看.话不多说,还是言归正传吧. 一.前言 我很喜欢电脑,可是笔记本还是太大,笔记本电脑再小还是要 ...

  9. Android传感器概述(六)

    监视传感器事件 要监视原始的传感器数据,你须要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged().Andr ...

随机推荐

  1. phpcms栏目调用

    {loop subcat(0,0,0,$siteid) $r} {php $num++} <h3><a href="{$r[url]}">{$r[catna ...

  2. APUE《UNIX 环境高级编程》读后感

    今天终于把APUE前17章全部看完了,基本上主要知识就在这些章节里. 之前看完<unix/linux编程实践教程>时,有一种豁然开朗.心旷神怡的感觉,在代码级别了解了linux很多系统机制 ...

  3. 【转载】之 破解 (【原创】Xenocode Postbuild 2009 加壳破解 (不断更新中...))

    声明 本文转载,感谢原作者dotNetSafe分享 [原创]Xenocode Postbuild 2009 加壳破解 (不断更新中...) http://bbs.pediy.com/showthrea ...

  4. hdfs 常用命令

    (2)bin/hdfs dfs -mkdir -p /home/雨渐渐 (3)scp /media/root/DCE28B65E28B432E/download/第2周/ChinaHadoop第二讲\ ...

  5. 设计模式之装饰者模式(Decorator Pattern)

    一.什么是装饰者模式? 装饰者模式能够完美实现“对修改关闭,对扩展开放”的原则,也就是说我们可以在不修改被装饰者的前提下,扩展被装饰者的功能. 再来看看我们的文件操作代码: 1 InputStream ...

  6. aix ipcs使用说明

    ipcs: -q   Writes information about active message queues -o             Writes the following usage ...

  7. TransactionScope IsolationLevel 事务隔离级别

    事务有四个特性 第一原子性,事务具有独立的不能被拆分的,不能只做部分,事务的性质是要么全做,要么都不做. 第二统一性,在事务执行之前和事务执行之后的数据是一致. 第三隔离性,事务是独立的,开发者不能查 ...

  8. Naive and Silly Muggles (计算几何)

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  9. 树形结构的维护:BZOJ 3991: [SDOI2015]寻宝游戏

    Description 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可 ...

  10. JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别

    整理翻译自:http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/ 大意是: 在J ...