Android 各種傳感器的使用

Android 各種傳感器的使用

這裡寫圖片描述

G-sensor:




    

package shortcut.song.com.myapplication;

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.widget.TextView;

public class AccelerometerActivity extends AppCompatActivity {
    SensorManager sensorManager;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accelerometer);

        textView = (TextView)findViewById(R.id.accelerometer_show);
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
                ,SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        super.onStop();
        sensorManager.unregisterListener(sensorEventListener);
    }

    SensorEventListener sensorEventListener = new SensorEventListener() {
        // 當傳感器的值發生變化時回調該方法
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            StringBuilder sb = new StringBuilder();
            sb.append("X:");
            sb.append(values[0]);
            sb.append("\nY:");
            sb.append(values[1]);
            sb.append("\nZ:");
            sb.append(values[2]);
            textView.setText(sb.toString());
        }

        // 傳感器精度改變時回調此方法
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

}

監聽多種Sensor:

package shortcut.song.com.myapplication;

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;

public class MoreSensorActivity extends AppCompatActivity {

    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_sensor);

        // 獲取傳感器管理服務
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 程序退出時 註銷傳感器監聽器
        sensorManager.unregisterListener(sensorEventListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 程序暫停時 註銷傳感器監聽器
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 方向傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 陀螺儀傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 磁場傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 重力傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 線性加速度傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 溫度傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 環境光傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 壓力傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)
                ,SensorManager.SENSOR_DELAY_GAME);
        // 加速度傳感器註冊監聽
        sensorManager.registerListener(sensorEventListener
                ,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
                ,SensorManager.SENSOR_DELAY_GAME);

    }

    SensorEventListener sensorEventListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] vaules = event.values;
            int sensorType = event.sensor.getType();
            StringBuilder sb = null;
            switch (sensorType) {
                case Sensor.TYPE_ORIENTATION:
                    sb = new StringBuilder();
                    sb.append("Z:"+ vaules[0]);
                    sb.append("\nX:"+ vaules[1]);
                    sb.append("\nY:"+ vaules[2]);
                    break;

                case Sensor.TYPE_GYROSCOPE:
                    sb = new StringBuilder();
                    sb.append("X:"+ vaules[0]);
                    sb.append("\nY:" + vaules[1]);
                    sb.append("\nZ:" + vaules[2]);
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    sb = new StringBuilder();
                    sb.append("X:"+ vaules[0]);
                    sb.append("\nY:" + vaules[1]);
                    sb.append("\nZ:" + vaules[2]);
                    break;
                case Sensor.TYPE_GRAVITY:
                    sb = new StringBuilder();
                    sb.append("Z:"+vaules[0]);
                    sb.append("\nY:"+vaules[1]);
                    sb.append("\nZ:"+vaules[2]);
                    break;
                case Sensor.TYPE_LINEAR_ACCELERATION:
                    sb = new StringBuilder();
                    sb.append("X:" + vaules[0]);
                    sb.append("\nY:" + vaules[1]);
                    sb.append("\nZ:" + vaules[2]);
                    break;
                case Sensor.TYPE_TEMPERATURE:
                    sb = new StringBuilder();
                    sb.append("Temp:" + vaules[0]);
                    break;
                case Sensor.TYPE_LIGHT:
                    sb = new StringBuilder();
                    sb.append("Light:" + vaules[0]);
                    break;
                case Sensor.TYPE_PRESSURE:
                    sb = new StringBuilder();
                    sb.append("Press:"+vaules[0]);
                    break;

                default:
                    break;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
}

You May Also Like