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. hihoCoder 1595 : Numbers

    Description You are given n constant integers c[1], c[2], ..., c[n] and an integer k. You are to ass ...

  2. 【BZOJ3224】【tyvj1728】普通平衡树

    最近开始学习平衡树,在学长的强烈推荐下学习了AVL.红黑树.splay(以上我都还没学)treap. 首先讲一下个人对treap(树堆)的理解. treap,顾名思义,就是tree+heap,首先因为 ...

  3. ●BZOJ 2743 [HEOI2012]采花

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 题解: 树状数组,离线 求区间里面有多少种出现次数大于等于 2 的颜色. 类似某一个题 ...

  4. 【网络流问题·我就想建好模】

    ·为了有助于你读后文,在写题前先列出一些大米饼的代码习惯: 一个提醒:所有的ADD函数无特殊说明均如图:(没有w就直接跳过) 以及: go(i,a,b)=====for(int i=a;i<=b ...

  5. hdu 1402 FFT(模板)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. bzoj3173[Tjoi2013]最长上升子序列 平衡树+lis

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2253  Solved: 1136[Submit][S ...

  7. 试说明采用双缓冲技术如何进行I/O操作

    输入设备先将第一个缓冲区装满数据,在输入设备向第二个缓冲区装数据时,处理机就可以从第一个缓冲区取出数据进行处理:当一个缓冲区的数据处理完毕,若第二个缓冲区已经装满,则处理机又可以从第二个缓冲区取出数据 ...

  8. MySQL 内连接与外连接

    1.内连接 MySQL中,join,cross join,inner join 是等价的. 2.外连接 2.1 左外连接 left join 2.2 右外连接  right join 3.连接条件 使 ...

  9. js去掉最后一个字符

    console.log(("0,1,2,3,4,5,".slice(0,-1)))

  10. Timestamp转Calendar

    Timestamp scheduleTime = r.getTimestamp("time_recv"); Calendar calendarScheduleTime = Cale ...