Android之使用传感器获取相应数据
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之使用传感器获取相应数据的更多相关文章
- android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...
- (转)android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection 复制代码 ...
- android客户端从服务器端获取json数据并解析的实现代码(重要)
首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...
- android通过httpClient请求获取JSON数据并且解析
使用.net创建一个ashx文件,并response.write json格式 public void ProcessRequest(HttpContext context) { context.R ...
- Android 将从网络获取的数据缓存到私有文件
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- ListView获取网络数据并展示优化练习
权限: <uses-permission android:name="android.permission.INTERNET"></uses-permission ...
- Android网络开发之实时获取最新数据
在实际开发中更多的是需要我们实时获取最新数据,比如道路流量.实时天气信息等,这时就需要通过一个线程来控制视图的更新. 示例:我们首先创建一个网页来显示系统当前的时间,然后在Android程序中每隔5秒 ...
- Android中通过代码获取arrays.xml文件中的数据
android工程res/valuse文件夹下的arrays.xml文件中用于放各种数组数据,比如字符串数组.整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用,下面针对这两种情况通过 ...
- Android中获取网络数据时的分页加载
//此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载, 蓝色部分是睡眠时间,自我感觉不用写 ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...
随机推荐
- react系列笔记1 用npx npm命令创建react app
react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...
- WEB前端移动开发初始化
meta篇 1.视窗宽度 <meta name="viewport" content="width=device-width,initial-scale=1.0,m ...
- HDU 3172 Virtual Friends (map+并查集)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- SQL 语法笔记
➪SQL ➪基本类型 char / varchar / int / smallint / numeric / real, double precision / float ➪数据定义 create t ...
- Python+OpenCV图像处理(七)—— 滤波与模糊操作
过滤是信号和图像处理中基本的任务.其目的是根据应用环境的不同,选择性的提取图像中某些认为是重要的信息.过滤可以移除图像中的噪音.提取感兴趣的可视特征.允许图像重采样等等.频域分析将图像分成从低频到高频 ...
- 大数据学习路线:Hadoop集群同步技术分享
今天给大家带来的技术分享是——Hadoop集群同步. 一.同步方式 选择一个机器,作为时间服务器(这里选择hadoop01),所有的机器与这台集群时间进行定时的同步,比如,每隔十分钟,同步一次时间. ...
- django rest framework跨表取值
- django 函数装饰器 变为 类装饰器
aaa
- 作为phper既然了解共享内存函数shmop的使用方法,那么就必须要了解一下信号量是什么,以及信号量使用的代码案例
在单独的一个PHP进程中读写.创建.删除共享内存方面上你应该没有问题了.但是实际运行中不可能只是一个PHP进程在运行中.如果在多个进程的情况下你还是沿用单个进程的处理方法,你一定会碰到问题--著名的并 ...
- 动态规划之115 Distinct Subsequences
题目链接:https://leetcode-cn.com/problems/distinct-subsequences/description/ 参考链接:https://www.cnblogs.co ...