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

第一步:创建一个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. C#【数据库】 Access类

    using System; using System.Data; using System.Data.OleDb; namespace AccessDb { /**//// <summary&g ...

  2. linux c数据库备份第一版

    使用linuxC实现的mysql数据库备份目标:通过alarm信号定时备份数据库备注:目前是第一个版,本身不能定时备份可以结合linux自动化实现定时备份.运行平台:Linux或类unix测试平台:u ...

  3. 【深入浅出jQuery】源码浅析2--使用技巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  4. AsyncCalls – Asynchronous function calls

    AsyncCalls – Asynchronous function callsWith AsyncCalls you can execute multiple functions at the sa ...

  5. BAT 批处理实现循环备份N天文件夹

    @echo off set today=%date:~0,4%%date:~5,2%%date:~8,2% xcopy  /E /I E:\aaa e:\test\%today% for /f &qu ...

  6. Unity NGUI制作scroll view

    unity版本:4.5 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/monzart7an/article/details/23878505,作者:CSDN 冬菊子   ...

  7. 高等数学(拉格朗日乘子法):NOI 2012 骑行川藏

    [NOI2012] 骑行川藏 输入文件:bicycling.in   输出文件:bicycling.out   评测插件 时间限制:1 s   内存限制:128 MB NOI2012 Day1 Des ...

  8. 动态规划(背包问题):POJ 1742 Coins

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32955   Accepted: 11199 Descripti ...

  9. 【模拟】FOJ 2244 Daxia want to buy house

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2244 题目大意: 每月还款额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1 ...

  10. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...