Android的大部分手机中都有传感器,传感器类型有方向、加速度(重力)、光线、磁场、距离(临近性)、温度等。

方向传感器:   Sensor.TYPE_ORIENTATION

加速度(重力)传感器: Sensor.TYPE_ACCELEROMETER

光线传感器:    Sensor.TYPE_LIGHT

磁场传感器:   Sensor.TYPE_MAGNETIC_FIELD

距离(临近性)传感器: Sensor.TYPE_PROXIMITY

温度传感器:   Sensor.TYPE_TEMPERATURE

//获取某种类型的感应器

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

//注册监听,获取传感器变化值

sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);

上面第三个参数为采样率:最快、游戏、普通、用户界面。当应用程序请求特定的采样率时,其实只是对传感器子系统的一个建议,不保证特定的采样率可用。

最快: SensorManager.SENSOR_DELAY_FASTEST

最低延迟,一般不是特别敏感的处理不推荐使用,该种模式可能造成手机电力大量消耗,由于传递的为原始数据,算法不处理好将会影响游戏逻辑和UI的性能。

游戏: SensorManager.SENSOR_DELAY_GAME

游戏延迟,一般绝大多数的实时性较高的游戏都使用该级别。

普通: SensorManager.SENSOR_DELAY_NORMAL

标准延迟,对于一般的益智类或EASY级别的游戏可以使用,但过低的采样率可能对一些赛车类游戏有跳帧现象。

用户界面: SensorManager.SENSOR_DELAY_UI

一般对于屏幕方向自动旋转使用,相对节省电能和逻辑处理,一般游戏开发中我们不使用。

使用传感器做应用的难点在于获取数据后如何处理,获得相应需要的效果,这里涉及很多数学物理的知识……

下面使用一个代码实例演示如何获取方向和磁场强度的数据:

package com.magnetic;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView magneticView;
private TextView orientationView;
private TextView totalMageticView;
private SensorManager sensorManager;
private MySensorEventListener sensorEventListener; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sensorEventListener = new MySensorEventListener();
magneticView = (TextView) this.findViewById(R.id.magneticView);
orientationView = (TextView) this.findViewById(R.id.orientationView);
totalMageticView=(TextView) this.findViewById(R.id.totalMageticView);
//获取感应器管理器
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume()
{
//获取方向传感器
@SuppressWarnings("deprecation")
Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sensorManager.registerListener(sensorEventListener, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL); //获取加速度传感器
Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} private final class MySensorEventListener implements SensorEventListener
{
//可以得到传感器实时测量出来的变化值
@Override
public void onSensorChanged(SensorEvent event)
{
//得到方向的值
if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)
{
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
orientationView.setText("方向: " + x + ", " + y + ", " + z);
}
//得到加速度的值
else if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD)
{
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
magneticView.setText("合磁场强度X、Y、Z分量: " + x + ", " + y + ", " + z);
//计算和磁场强度
float total=(float)(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z, 2)));
totalMageticView.setText("合磁场强度: " + total);
}
}
//重写变化
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
} //暂停传感器的捕获
@Override
protected void onPause()
{
sensorManager.unregisterListener(sensorEventListener);
super.onPause();
} }
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TableRow>
<TextView
android:id="@+id/orientationView"
android:text="方向传感器:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/magneticView"
android:text="磁传感器:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/totalMageticView"
android:text="合磁强度:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>

Android之使用传感器获取相应数据的更多相关文章

  1. android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下   首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...

  2. (转)android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下       首先客户端从服务器端获取json数据 1.利用HttpUrlConnection   复制代码 ...

  3. android客户端从服务器端获取json数据并解析的实现代码(重要)

    首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...

  4. android通过httpClient请求获取JSON数据并且解析

    使用.net创建一个ashx文件,并response.write  json格式 public void ProcessRequest(HttpContext context) { context.R ...

  5. Android 将从网络获取的数据缓存到私有文件

    1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...

  6. ListView获取网络数据并展示优化练习

    权限: <uses-permission android:name="android.permission.INTERNET"></uses-permission ...

  7. Android网络开发之实时获取最新数据

    在实际开发中更多的是需要我们实时获取最新数据,比如道路流量.实时天气信息等,这时就需要通过一个线程来控制视图的更新. 示例:我们首先创建一个网页来显示系统当前的时间,然后在Android程序中每隔5秒 ...

  8. Android中通过代码获取arrays.xml文件中的数据

    android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组.整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过 ...

  9. Android中获取网络数据时的分页加载

    //此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载,    蓝色部分是睡眠时间,自我感觉不用写  ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...

随机推荐

  1. JS 测试网络速度与网络延迟

    一.延迟与网速 通过js加载一张1x1的极小图片,测试出图片加载的所用的时长.如果换一个几百KB的图片,则可心用来计算下载网速 document.write('<input type=" ...

  2. Codeforces 124A - The number of positions

    题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't ...

  3. @RefreshScope 的作用

    让在application.properties里自定义的变量也能通过@Value 注解正常注入

  4. navicat mysql导出数据 批量插入的形式

    这里介绍的是mysql 相同服务器类型数据传输的高级设置 选中数据库后右键“ 转储SQL文件”默认导出的记录格式是一条条的,采用的是”完整插入语句”,格式如下 '); '); '); 这种格式保证了兼 ...

  5. git获取一个版本相对于另一个版本新增,修改,删除的文件

    git diff --name-status 00ef237ef0f0a4b8bd9609c2b6d570472028212d abf13b4d58abbb05a7d494cdc205d025978a ...

  6. mycat->oracle报java.sql.SQLException: 无法从套接字读取更多的数据

    今天下午,测试环境清算的时候又出现了之前的一个异常,这次把错误信息全部打出来了,java.sql.SQLException: 无法从套接字读取更多的数据,是使用mycat连接oracle的,如下所示: ...

  7. Redhat7.4 vim编辑无颜色解决

    背景 发现使用vim时, 统一颜色, 不区分关键字 解决 vim /etc/profile 在文件的末尾加入下面这一行 export TERM=xterm

  8. 集合框架-Set集合

    代码: Collection c = new ArrayList(); c.add("hello"); c.add("world"); c.add(" ...

  9. Ubuntu + CUDA9 + CUDNN7 + OpenCV3.4 + contrib +CAFFE-master

    安装ubuntu时赞美Rufus(建议ubuntu16.04.04),过程参考 https://www.cnblogs.com/willnote/p/6725594.html 安 装 前 一 定 要 ...

  10. 【python009--循环】

    1.for循环语法: for  目标  in 表达式: 循环体 2.range() 语法:range([start] stop [, step = 1]) --这个bif有三个参数,其中用中括号括起来 ...