Android加速度传感器

效果图

手机平放桌面的两张截屏,数据一直在刷新



源码

下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9065313

步骤

传感器使用步骤之前已经介绍过,地址:http://blog.csdn.net/q4878802/article/details/48112477

代码

package com.example.kongqw.kqwsensorforaccelerometerdemo;

import android.app.Activity;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView; public class MainActivity extends Activity implements SensorEventListener { private TextView mTvShow;
private SensorManager mSensorManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTvShow = (TextView) findViewById(R.id.tv_show); // 获取传感器管理者对象
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // 获取加速度传感器对象
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // 添加监听器
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI); } @Override
public void onSensorChanged(SensorEvent event) {
// 传感器返回的数据
float[] values = event.values;
StringBuffer buffer = new StringBuffer();
buffer.append("X方向的加速度为:").append(values[0]).append("\n");
buffer.append("Y方向的加速度为:").append(values[1]).append("\n");
buffer.append("Z方向的加速度为:").append(values[2]).append("\n");
mTvShow.setText(buffer);
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
}

XML页面布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity"> <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="加速度传感器"
android:textSize="20dp" /> <TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title"
android:textSize="18dp" /> </RelativeLayout>

Android加速度传感器的更多相关文章

  1. Android加速度传感器实现“摇一摇”,带手机振动

    由于代码有点多,所以就分开写了,注释还算详细,方便学习 Activity package com.lmw.android.test;   import android.app.Activity; im ...

  2. android 加速度传感器 ---摇一摇

    package com.eboy.testyaoyiyao;import java.text.SimpleDateFormat;import java.util.Date;import android ...

  3. Android--保持加速度传感器在屏幕关闭后运行

    由于写论文需要,需要用手机加速度采集数据,关于android加速度传感器的介绍网上一抓一大把,但大多都是大同小异,跟官网文档差不多.自己写了个取加速度传感器的APK,发现数据有点不对劲,原理屏幕一关后 ...

  4. Android--保持加速度传感器在屏幕关闭后运行(收集)

    由于写论文需要,需要用手机加速度采集数据,关于android加速度传感器的介绍网上一抓一大把,但大多都是大同小异,跟官网文档差不多.自己写了个取加速度传感器的APK,发现数据有点不对劲,原理屏幕一关后 ...

  5. 玩转Android之加速度传感器的使用,模仿微信摇一摇

    Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...

  6. Android的重力传感器(3轴加速度传感器)简单实例

    重力感应主要是依靠手机的加速度传感器(accelerometer)来实现 在Android的开发中一共有八种传感器但是不一定每一款真机都支持这些传感器.因为很多功能用户根本不care的所以可能开发商会 ...

  7. Android传感器——加速度传感器

    步骤如下: 1. 调用Context的getSystemService(Context.SENSOR_SERVICE)方法获取SensorManager,SensorManager对象代表系统的传感器 ...

  8. Android 使用加速度传感器实现摇一摇功能及优化

    如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得 ...

  9. Android指南针之加速度传感器地磁传感器-android学习之旅(67)

    由于andorid不推荐用传统的方向传感器,推荐用加速度传感器和地磁传感器来构造得到方向传感器的数据,其实主要是z轴的旋转角度 具体代码示例 代码如下 public class MainActivit ...

随机推荐

  1. SRM 558 SurroundingGame

    题意: 给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取  2.邻近方格都被选取(有公共边被称为邻近)  不一定要占据所有方格,求最大收益. 输入 ...

  2. UVA 11584 划分回文字串

    将其划分为尽可能少的回文串 dp[i] = min(dp[i],dp[j] + 1)    来表示j+1~i是回文串 #include <iostream> #include <cs ...

  3. hdu 5573Binary Tree

    Binary Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  4. const的一些用法和理解

    首先先说一下const常量的用处,我们知道宏定义#define是没有数据类型的,编译器在编译的时候,不会对宏常量进行类型检查,只进行简单的字符串替换,字符串替换时极易产生意想不到的错误,所以这个时候, ...

  5. Machine Learning From Scratch-从头开始机器学习

    Python implementations of some of the fundamental Machine Learning models and algorithms from scratc ...

  6. SecureFX连接Linux后文件夹中文乱码问题解决(转)

    在使用SecureFX 连接Linux 时,发现文件夹显示乱码,一直尝试各种配置,现将方法整理一下!供大家参考! 首先在选项中设置字符编码为UTF-8 然后在全局选项中找到Securefx的配置文件 ...

  7. gulp填坑记(二)——gulp多张图片自动合成雪碧图

    为优化图片,减少请求会把拿到切好的图标图片,通过ps(或者其他工具)把图片合并到一张图里面,再通过css定位把对于的样式写出来引用的html里面,对于一些图片较多的项目,这个过程可能要花费我们一天的时 ...

  8. 反向Ajax之Socket.io

    1.什么是反向ajax? 传统的ajax的困惑? 新需求--当服务器端数据发生变化时,客户端(浏览器端)如何即时得到通知呢? 找一些实际的案例:客服系统.在线聊天 这类应用,有一个显著的特点: 数据并 ...

  9. 数据库4m10d作业

    Create table student ( Sno char(15) primary key , Sname varchar(10) not null, Sage tinyint , Special ...

  10. Jupyter Notebook

    Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...