Android 開發 — 顯示手機傳感器

Android 開發包標準有8個傳感器:

 

  • Sensor.TYPE_ACCELEROMETER
    • 加速度計 (X, Y, Z) m/s2
  • Sensor.TYPE_GYROSCOPE
    • 陀螺儀 (X, Y, Z) degrees
  • Sensor.TYPE_LIGHT
    • 光照 (single) lux
  • Sensor.TYPE_MAGNETIC_FIELD
    • 磁力計 (X, Y, Z) microteslas
  • Sensor.TYPE_ORIENTATION
    • 方位傳感器 (X, Y, Z) degrees
  • Sensor.TYPE_PRESSURE
    • 壓力傳感器 (single) kilopascals 測量加在手機設備上的壓力
  • Sensor.TYPE_PROXIMITY
    • 距離傳感器 (single) meters 典型應用爲接聽電話時,根據光照,聲音估計距離
  • Sensor.TYPE_TEMPERATURE
    • 溫度傳感器 (single) degrees Celsius 電池溫度,或是具體傳感器溫度,看具體實現

手機型號不同,硬件實現有所區別。

 

讀取傳感器代碼如下:

package com.ldq.sensor;

import java.util.List;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExSensor extends Activity {

	private LinearLayout layout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		layout = (LinearLayout) findViewById(R.id.LinearLayout01);

		SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		List<Sensor> list = sm.getSensorList(Sensor.TYPE_ALL);
		TextView text = new TextView(this);
		text.setText("傳感器數量:" + list.size());
		layout.addView(text);

		TextView[] name = new TextView[list.size()];
		for (int i = 0; i < list.size(); i++) {
			name[i] = new TextView(this);
			name[i].setText((i + 1) + " : " + list.get(i).getName());
			layout.addView(name[i]);
		}

	}
}

 

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