Android加速度传感器

效果图

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

Android加速度传感器_第1张图片
Android加速度传感器_第2张图片

源码

下载地址(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>

更多相关文章

  1. Android源码分析(四)-----Android源码编译及刷机步骤
  2. Android中手机声音调节步骤(Android学习随笔四)
  3. 创建 cocos2d-x+lua for android 步骤
  4. android开发步骤简要笔记
  5. Android传感器介绍
  6. Android中回调下载事件处理实现步骤
  7. android数字证书-签名(步骤)
  8. Android传感器(第一篇)
  9. 《Android传感器高级编程》

随机推荐

  1. Java + TestNG + Appium 实现单机多个And
  2. Android 开发中遇到的 bug(8)
  3. Android 短信数据库详细总结分析
  4. Android 使用自定义注解代替重复写findVi
  5. android /system/lib/ so库解析
  6. 【android】点击事件穿透解决方案
  7. Qt5.2发布了
  8. android MVP架构学习
  9. Android UI编程基础3
  10. Android第五十二期 - 云之讯的代码混淆