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

第一步:创建一个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. Linux 使用yum install安装mysql登陆不上解决办法

    CentOS yum安装mysql后 Can’t connect to local MySQL server through socket ‘/var/lib/ CentOS Can’t connec ...

  2. uboot main_loop函数分析

    一.概述    main_loop()函数做的都是与具体平台无关的工作.主要包括的工作如下: (1)初始化启动次数限制机制 (2)Modem功能 (3)设置软件版本号 (4)启动延迟 (5)读取命令, ...

  3. IOS网络编程:HTTP

    IOS网络编程:HTTP HTTP定义了一种在服务器和客户端之间传递数据的途径. URL定义了一种唯一标示资源在网络中位置的途径. REQUESTS 和 RESPONSES: 客户端先建立一个TCP连 ...

  4. Google为何这么屌

    概述: 在移动市场上,从来没有一个公司像Google一样的玩法,以后可能也不会有.因为这根本就不是一个商业模式.它可以调动极大的资源而几乎没有盈利压力,它力逾千钧又身段灵活.它从来不尊重这个市场原有的 ...

  5. The Child and Toy

    Codeforces Round #250 (Div. 2) C:http://codeforces.com/problemset/problem/437/C 题意:给以一个无向图,每个点都有一点的权 ...

  6. PHP打印各种金字塔!

    PHP打印各种金字塔! <?php for($i=1;$i<=7;$i++){ for($j=1;$j<=5-$i;$j++){ echo ''; } for($k=1;$k< ...

  7. 本地yum源

    <pre name="code" class="html">1.为DVD创建一个挂载目录 [root@localhost ~]# mkdir /me ...

  8. 实战weblogic集群之安装weblogic

    一.系统及软件版本 OS版本:Red Hat Enterprise Linux Server release 6.6WebLogic Server 版本: 10.3.3.0JDK版本:1.7.0_79 ...

  9. HDOJ(HDU) 2143 box(简单的多次判断-用的卫条件)

    Problem Description One day, winnie received a box and a letter. In the letter, there are three inte ...

  10. HDOJ 2096 小明A+B

    Problem Description 小明今年3岁了, 现在他已经能够认识100以内的非负整数, 并且能够进行100以内的非负整数的加法计算. 对于大于等于100的整数, 小明仅保留该数的最后两位进 ...