国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現獲取所有傳感器數據的方法

2019-12-12 02:46:26
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現獲取所有傳感器數據的方法。分享給大家供大家參考,具體如下:

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  ><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="加速度"  android:id="@+id/edt1"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="磁場"  android:id="@+id/edt2"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="定位"  android:id="@+id/edt3"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="陀螺儀"  android:id="@+id/edt4"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="光線"  android:id="@+id/edt5"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="壓力"  android:id="@+id/edt6"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="溫度"  android:id="@+id/edt7"  />    <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="距離"  android:id="@+id/edt8"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="重力"  android:id="@+id/edt9"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="線性加速度"  android:id="@+id/edt10"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="旋轉矢量"  android:id="@+id/edt11"  /><TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="defalut"  android:id="@+id/edt12"  /></LinearLayout>

main.java

/* * * IBMEyes.java * sample code for IBM Developerworks Article * Author: W. Frank Ableson * fableson@msiservices.com * */package com.msi.ibm.eyes;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import android.hardware.Sensor;import android.hardware.SensorManager;import android.hardware.SensorListener;public class IBMEyes extends Activity implements SensorListener {  final String tag = "IBMEyes";  SensorManager sm = null;  TextView View1 = null;  TextView View2 = null;  TextView View3 = null;  TextView View4 = null;  TextView View5 = null;  TextView View6 = null;  TextView View7 = null;  TextView View8 = null;  TextView View9 = null;  TextView View10 = null;  TextView View11 = null;  TextView View12 = null;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    sm = (SensorManager) getSystemService(SENSOR_SERVICE);    setContentView(R.layout.main);    View1 = (TextView) findViewById(R.id.edt1);    View2 = (TextView) findViewById(R.id.edt2);    View3 = (TextView) findViewById(R.id.edt3);    View4 = (TextView) findViewById(R.id.edt4);    View5 = (TextView) findViewById(R.id.edt5);    View6 = (TextView) findViewById(R.id.edt6);    View7 = (TextView) findViewById(R.id.edt7);    View8 = (TextView) findViewById(R.id.edt8);    View9 = (TextView) findViewById(R.id.edt9);    View10 = (TextView) findViewById(R.id.edt10);    View11 = (TextView) findViewById(R.id.edt11);    View12 = (TextView) findViewById(R.id.edt12);  }  public void onSensorChanged(int sensor, float[] values) {    synchronized (this) {      String str = "X:" + values[0] + ",Y:" + values[1] + ",Z:" + values[2];      switch (sensor){      case Sensor.TYPE_ACCELEROMETER:        View1.setText("加速度:" + str);        break;      case Sensor.TYPE_MAGNETIC_FIELD:        View2.setText("磁場:" + str);        break;      case Sensor.TYPE_ORIENTATION:        View3.setText("定位:" + str);        break;      case Sensor.TYPE_GYROSCOPE:        View4.setText("陀螺儀:" + str);        break;      case Sensor.TYPE_LIGHT:        View5.setText("光線:" + str);        break;      case Sensor.TYPE_PRESSURE:        View6.setText("壓力:" + str);        break;      case Sensor.TYPE_TEMPERATURE:        View7.setText("溫度:" + str);        break;      case Sensor.TYPE_PROXIMITY:        View8.setText("距離:" + str);        break;      case Sensor.TYPE_GRAVITY:        View9.setText("重力:" + str);        break;      case Sensor.TYPE_LINEAR_ACCELERATION:        View10.setText("線性加速度:" + str);        break;      case Sensor.TYPE_ROTATION_VECTOR:        View11.setText("旋轉矢量:" + str);        break;      default:        View12.setText("NORMAL:" + str);        break;      }    }  }  public void onAccuracyChanged(int sensor, int accuracy) {    Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);  }  @Override  protected void onResume() {    super.onResume();    sm.registerListener(this,        Sensor.TYPE_ACCELEROMETER |        Sensor.TYPE_MAGNETIC_FIELD |        Sensor.TYPE_ORIENTATION |        Sensor.TYPE_GYROSCOPE |        Sensor.TYPE_LIGHT |        Sensor.TYPE_PRESSURE |        Sensor.TYPE_TEMPERATURE |        Sensor.TYPE_PROXIMITY |        Sensor.TYPE_GRAVITY |        Sensor.TYPE_LINEAR_ACCELERATION |        Sensor.TYPE_ROTATION_VECTOR,        SensorManager.SENSOR_DELAY_NORMAL);  }  @Override  protected void onStop() {    sm.unregisterListener(this);    super.onStop();  }}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android開發入門與進階教程》、《Android編程之activity操作技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 怀来县| 莒南县| 茌平县| 宁强县| 高台县| 金溪县| 扎赉特旗| 海南省| 吉隆县| 新建县| 青浦区| 九台市| 浦城县| 龙岩市| 马龙县| 丹阳市| 高清| 安宁市| 托克逊县| 平南县| 米泉市| 信丰县| 通许县| 历史| 鄂托克前旗| 海淀区| 张掖市| 通化市| 明水县| 岱山县| 金寨县| 大丰市| 武夷山市| 临漳县| 涟水县| 丹巴县| 崇礼县| 崇礼县| 出国| 淳化县|