溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android應(yīng)用中實(shí)現(xiàn)如何獲取所有傳感器數(shù)據(jù)的

發(fā)布時(shí)間:2020-12-07 15:27:56 來源:億速云 閱讀:136 作者:Leah 欄目:移動(dòng)開發(fā)

這篇文章給大家介紹Android應(yīng)用中實(shí)現(xiàn)如何獲取所有傳感器數(shù)據(jù)的,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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="磁場(chǎng)"
  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="旋轉(zhuǎn)矢量"
  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("磁場(chǎng):" + 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("旋轉(zhuǎn)矢量:" + 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();
  }
}

關(guān)于Android應(yīng)用中實(shí)現(xiàn)如何獲取所有傳感器數(shù)據(jù)的就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI