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. Struts2--标签tag

    在Struts2框架中提供了一套标签库,可以与struts2无缝结合. 数据标签a.action.bean.date.debug.i18n.include.param.property.push.se ...

  2. [ZJOI2010]基站选址

    题目描述 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄不超过Si的范 ...

  3. [UOJ] #217. 【UNR #1】奇怪的线段树

    题解见大佬博客 我的丑陋代码: #include<cstdio> #include<cstring> #include<cstdlib> inline int re ...

  4. Codeforces 700E. Cool Slogans

    Description 给定一个串 \(S\),求一个序列 \(a_i\),满足 \(a_i\) 是原串的子串,且 \(a_i\) 在 \(a_{i-1}\) 中至少出现两次,求这个序列的最大的长度 ...

  5. NOIP 2013

    Prob.1 转圈游戏 找到循环节,然后快速幂.代码: #include<cstdio> #include<cstring> #include<iostream> ...

  6. Codeforces Round #398 (div.2)简要题解

    这场cf时间特别好,周六下午,于是就打了打(谁叫我永远1800上不去div1) 比以前div2的题目更均衡了,没有太简单和太难的...好像B题难度高了很多,然后卡了很多人. 然后我最后做了四题,E题感 ...

  7. bzoj3262陌上花开 cdq分治

    3262: 陌上花开 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2794  Solved: 1250[Submit][Status][Discus ...

  8. 配置mysql使其允许外部ip进行登录

    这两天在做一个数据库的项目,用到了mysql,需要通过外部的ip远程访问mysql,发现默认的mysql是不允许远程访问的,需要做简单的配置. 如下: 1. 打开一个终端(cmd)输入 mysql - ...

  9. Python中str字符串的功能介绍

    Str字符串的功能介绍 1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + st ...

  10. ChatGirl is an AI ChatBot based on TensorFlow Seq2Seq Model

    Introduction [Under developing,it is not working well yet.But you can just train,and run it.] ChatGi ...