本文主要是介绍android 传感器 Sensor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/SensorTest/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/orientation" /><EditTextandroid:id="@+id/etOrientation"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/magnetic" /><EditTextandroid:id="@+id/etMagnetic"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/temperature" /><EditTextandroid:id="@+id/etTemerature"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/light" /><EditTextandroid:id="@+id/etLight"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/pressure" /><EditTextandroid:id="@+id/etPressure"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false" /></LinearLayout>
/SensorTest/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="action_settings">Settings</string><string name="hello_world">Hello world!</string><string name="hello">Hello World, AccelerometerTest!</string><string name="app_name">传感器模拟器</string><string name="orientation">方向传感器返回的值</string><string name="magnetic">磁场传感器返回的值</string><string name="temperature">温度传感器返回的值</string><string name="light">光传感器返回的值</string><string name="pressure">压力传感器返回的值</string>
</resources>
/SensorTest/src/com/lxm/sensortest/MainActivity.java
package com.lxm.sensortest;import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;public class MainActivity extends Activity implements SensorEventListener {// 定义Sensor管理器private SensorManager mSensorManager;EditText etOrientation;EditText etMagnetic;EditText etTemerature;EditText etLight;EditText etPressure;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取界面上的EditText组件etOrientation = (EditText) findViewById(R.id.etOrientation);etMagnetic = (EditText) findViewById(R.id.etMagnetic);etTemerature = (EditText) findViewById(R.id.etTemerature);etLight = (EditText) findViewById(R.id.etLight);etPressure = (EditText) findViewById(R.id.etPressure);// 获取传感器管理服务mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);}@Overrideprotected void onResume() {super.onResume();// 为系统的方向传感器注册监听器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);// 为系统的磁场传感器注册监听器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_GAME);// 为系统的温度传感器注册监听器mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),SensorManager.SENSOR_DELAY_GAME);// 为系统的光传感器注册监听器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),SensorManager.SENSOR_DELAY_GAME);// 为系统的压力传感器注册监听器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),SensorManager.SENSOR_DELAY_GAME);}@Overridepublic 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;}@Overridepublic void onSensorChanged(SensorEvent event) {float[] values = event.values;// 获取触发event的传感器类型int sensorType = event.sensor.getType();StringBuilder sb = null;switch (sensorType) {// 方向传感器case Sensor.TYPE_ORIENTATION:sb = new StringBuilder();sb.append("绕Z轴转过的角度:");sb.append(values[0]);sb.append("\n绕X轴转过的角度:");sb.append(values[1]);sb.append("\n绕Y轴转过的角度:");sb.append(values[2]);etOrientation.setText(sb.toString());break;// 磁场传感器case Sensor.TYPE_MAGNETIC_FIELD:sb = new StringBuilder();sb.append("X方向上的角度:");sb.append(values[0]);sb.append("\nY方向上的角度:");sb.append(values[1]);sb.append("\nZ方向上的角度:");sb.append(values[2]);etMagnetic.setText(sb.toString());break;// 温度传感器case Sensor.TYPE_AMBIENT_TEMPERATURE:sb = new StringBuilder();sb.append("当前温度为:");sb.append(values[0]);etTemerature.setText(sb.toString());break;// 光传感器case Sensor.TYPE_LIGHT:sb = new StringBuilder();sb.append("当前光的强度为:");sb.append(values[0]);etLight.setText(sb.toString());break;// 压力传感器case Sensor.TYPE_PRESSURE:sb = new StringBuilder();sb.append("当前压力为:");sb.append(values[0]);etPressure.setText(sb.toString());break;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}@Overrideprotected void onStop() {// 程序退出时取消注册传感器监听器mSensorManager.unregisterListener(this);super.onStop();}@Overrideprotected void onPause() {// 程序暂停时取消注册传感器监听器mSensorManager.unregisterListener(this);super.onPause();}}
效果图:
源码下载
这篇关于android 传感器 Sensor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!