Android學習之傳感器的簡單使用

Android學習之傳感器的簡單使用

  1. SensorManager類
  SensorManager類用來管理各個傳感器:通過SensorManager創建實例,並用getSystemService(SENSOR_SERVICE)獲取傳感器服務。
  使用其getSensorList()方法,可以獲取所有可用的傳感器該方法返回一個List<Sensor>,即Sensor對象的列表。
  注意:當不使用或Activity暫停的時候,要關閉感應器:屏幕關閉時,系統不會自動關閉感應器,這會導致耗電增加,關閉的方法,就是解除對傳感器的監聽。

  2. Sensor類
  Sensor實例對應一個具體的傳感器,通過判斷Sensor的類型,來處理器傳感器信息,類型如下:
  方向傳感器(Orientation sensor):SENSOR_TYPE_ORIENTATION
  加速感應器(Accelerometer sensor):SENSOR_TYPE_ACCELEROMETER
  陀螺儀傳感器(Gyroscope sensor):SENSOR_TYPE_GYROSCOPE
  磁場傳感器(Magnetic field sensor):SENSOR_TYPE_MAGNETIC_FIELD
  接近(距離)感應器(Proximity sensor):SENSOR_TYPE_PROXIMITY
  光線傳感器(Light sensor):SENSOR_TYPE_LIGHT
  氣壓傳感器(Pressure sensor):SENSOR_TYPE_PRESSURE
  溫度傳感器(Temperature sensor): SENSOR_TYPE_TEMPERATURE
  重力感應器(Gravity sensor,Android 2.3引入):SENSOR_TYPE_GRAVITY
  線性加速感應器(Linear acceleration sensor ,Android 2.3引入):SENSOR_TYPE_LINEAR_ACCELERATION
  旋轉矢量傳感器(Rotation vector sensor,Android 2.3引入): SENSOR_TYPE_ROTATION_VECTOR
  相對溼度傳感器(Relative humidity sensor,Android 4.0引入)
  近場通信(NFC)傳感器(Android 2.3引入),NFC和其他不一樣,具有讀寫功能。

  3. SensorEventListener 監聽器
  使用SensorEventListener可以監聽傳感器的各種事件,主要使用onSensorChanged()事件來獲取傳感器的信息。
  onSensorChanged()事件的參數爲SensorEvent對象,SensorEvent包含以下信息:
  · 傳感器類型: Sensor sensor
  · 傳感器數值精度:int accuracy
  · 傳感器具體值:float[ ] values
  通過訪問SensorEvent中的信息來獲取具體數值。

  4. 使用傳感器的步驟:
  · 1. 定義SensorManager,並獲取SensorManager實例;
  · 2. 定義Sensor,並指定傳感器;
  · 3. 爲定義的傳感器註冊事件監聽事件:sensorManager.registerListener(三個參數),三個參數分別爲:SensorEventListener、Sensor、更新速率;
  · 4. 創建SensorEventListener監聽器,獲取傳感器的值;
  · 5. 退出應用時,應註銷傳感器事件的監聽:sensorManager.unregisterListener(Sensor sensor)。

  ☆☆☆Android Studio實現在加速度傳感器的使用
  1.打開Android Studio,新建工程後,在activity_main.xml中界添加一個按鈕和三個TextView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="lession.example.com.androidlession616.MainActivity">
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <Button
            android:text="使用三軸加速度感應器(重力)"
            android:layout_width="356dp"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="20sp" />
            
        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button"
            android:id="@+id/textView"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="20sp" />
            
        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="20sp" />
            
        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView3"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="20sp" />
            
    </LinearLayout>
    
</RelativeLayout>

  2.在MainActivity中,編寫代碼。

package lession.example.com.androidlession616;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tv1,tv2,tv3;
    private float x, y, z;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt = (Button) findViewById(R.id.button);
        tv1 = (TextView) findViewById(R.id.textView);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通過服務得到傳感器管理對象
                SensorManager sensorMgr = (SensorManager)
                        getSystemService(SENSOR_SERVICE);
                //得到重力傳感器實例
                //TYPE_ACCELEROMETER 加速度傳感器(重力傳感器)類型。
                //TYPE_ALL 描述所有類型的傳感器。
                //TYPE_GYROSCOPE 陀螺儀傳感器類型
                //TYPE_LIGHT 光傳感器類型
                //TYPE_MAGNETIC_FIELD 恆定磁場傳感器類型。
                //TYPE_ORIENTATION 方向傳感器類型。
                //TYPE_PRESSURE 描述一個恆定的壓力傳感器類型
                //TYPE_PROXIMITY 常量描述型接近傳感器
                //TYPE_TEMPERATURE 溫度傳感器類型描述
                final Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                SensorEventListener lsn = new SensorEventListener() {
                    @SuppressWarnings("deprecation")//表示不檢測過期的方法
                    //傳感器獲取值改變時響應此函數
                    public void onSensorChanged(SensorEvent e) {
                        x = e.values[0];
                        y = e.values[1];
                        z = e.values[2];
//                        x = e.values[SensorManager.DATA_X];
//                        y = e.values[SensorManager.DATA_Y];
//                        z = e.values[SensorManager.DATA_Z];
                        tv1.setText("x=" + x );//手機水平放置,左右x值
                        tv2.setText("y=" + y );//手機水平放置,前後y值
                        tv3.setText("z=" + z );//手機豎直放置,上下z值
                    }
                    public void onAccuracyChanged(Sensor s, int accuracy) {
                    }
                };
                //註冊listener,第三個參數是檢測的精確度
                sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
            }
        });
    }
}

  運行結果:
  在這裏插入圖片描述

  ☆☆☆Android Studio實現在光線傳感器的使用
  1.打開Android Studio,新建工程後,在activity_main.xml中界添加一個按鈕和一個TextView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="lession.example.com.androidlession616_2.MainActivity">
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <Button
            android:text="光照強度"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="20sp" />
            
        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="20sp" />
            
    </LinearLayout>
    
</RelativeLayout>

  2.在MainActivity中,編寫代碼。

package lession.example.com.androidlession616_2;

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.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv = (TextView) findViewById(R.id.textView);
        Button bt = (Button) findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SensorManager mSManager = (SensorManager)
                        getSystemService(Context.SENSOR_SERVICE);
                Sensor mSen = mSManager.getDefaultSensor(Sensor.TYPE_LIGHT);
                SensorEventListener mSEListener = new SensorEventListener() {
                    @Override
                    public void onSensorChanged(SensorEvent event) {
                        tv.setText("光照強度爲:\n"+event.values[0]+"勒克斯");
                    }
                    @Override
                    public void onAccuracyChanged(Sensor sensor, int accuracy) {
                    }
                };
                mSManager.registerListener(mSEListener,mSen,SensorManager.SENSOR_DELAY_NORMAL);
            }
        });
    }
}

  運行結果:
  在這裏插入圖片描述
  這就是傳感器的簡單使用,如果轉載以及CV操作,請務必註明出處,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章