Android---57---感應器,android感應器

來源:互聯網
上載者:User

Android---57---感應器,android感應器

Android系統提供了對感應器的支援。
開發應用感應器很簡單,只需要為指定監聽器註冊一個監聽器即可。


步驟:
1.調用Context的getSystemService(Context.SENSOR_SERVICE)方法 擷取SensorManager對象,SensorManager對象代表系統的感應器管理服務
2.調用SensorManager的getDefaultSensor(int type)方法來擷取指定類型的感應器
3.通常選擇在Activity的onResume()方法中調用SensorManager的registerListener()為指定感測去註冊監聽即可。
程式通過實現監聽器即可擷取感應器傳回來的資料。

SensorManager提供的註冊感應器的方法為:registerListener(SensorEventListener listener,Sensor sensor ,int rate ):
參數:
listener:
監聽感應器的監聽器。該監聽器需要實現SensorEventListener介面
sensor:感測對象
rate:指定擷取感應器資料的頻率。
有如下幾個頻率:

SensorManager.SENSOR_DELAY_FASTEST:
最快,延遲最小,只有特別依賴於感應器資料的應用推薦採用這種頻率。

SensorManager.SENSOR_DELAY_GAME:
適合遊戲的頻率。在一般實用性的應用上適合使用這種頻率。

SensorManager.SENSOR_DELAY_NORMAL:
正常頻率。一般即時性要求不是特別高的應用上適合使用。

SensorManager.SENSOR_DELAY_UI:
適合普通使用者介面的頻率。普通小程式中使用。

 

 

Android常用的感應器:

方向、磁場、溫度、光、壓力。

 

 

 

布局檔案:


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    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="com.example.sensor.MainActivity" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/orientation" />    <EditText        android:id="@+id/etOrientation"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/magnetic" />    <EditText        android:id="@+id/etMagnetic"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/temperature" />    <EditText        android:id="@+id/etTemerature"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/light" />    <EditText        android:id="@+id/etLight"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/pressure" />    <EditText        android:id="@+id/etPressure"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false" /></LinearLayout>


 

Activity:

 

public class MainActivity extends Activity implements SensorEventListener {// 定義SensorManagerSensorManager mSensorManager;// 方向EditText etOrientation;// 磁場EditText etMagnetic;// 溫度EditText etTemerature;// 光EditText etLight;// 壓力EditText etPressure;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etOrientation = (EditText) findViewById(R.id.etOrientation);etMagnetic = (EditText) findViewById(R.id.etMagnetic);etTemerature = (EditText) findViewById(R.id.etTemerature);etLight = (EditText) findViewById(R.id.etLight);etPressure = (EditText) findViewById(R.id.etPressure);// 擷取感應器管理服務mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);}@Overrideprotected void onResume() {super.onResume();// 為方向感應器註冊監聽器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);// 為系統的磁場感應器註冊監聽器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_GAME);// 為系統的溫度感應器註冊監聽器mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),SensorManager.SENSOR_DELAY_GAME);// 為系統的光感應器註冊監聽器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),SensorManager.SENSOR_DELAY_GAME);// 為系統的壓力感應器註冊監聽器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onStop() {mSensorManager.unregisterListener(this);super.onStop();}@Overrideprotected void onPause() {mSensorManager.unregisterListener(this);super.onPause();}// 以下是實現SensorEventListener介面必須實現的方法@Overridepublic void onSensorChanged(SensorEvent event) {float values[] = event.values;int sensorType = event.sensor.getType();StringBuilder sb = new StringBuilder();switch (sensorType) {// 方向感應器case Sensor.TYPE_ORIENTATION:sb = new StringBuilder();sb.append("繞Z軸轉過的角度:");sb.append(values[0]);sb.append("\n繞X軸轉過的角度:");sb.append(values[1]);sb.append("\n繞Y軸轉過的角度:");sb.append(values[2]);etOrientation.setText(sb.toString());break;// 磁場感應器case Sensor.TYPE_MAGNETIC_FIELD:sb = new StringBuilder();sb.append("X方向上的角度:");sb.append(values[0]);sb.append("\nY方向上的角度:");sb.append(values[1]);sb.append("\nZ方向上的角度:");sb.append(values[2]);etMagnetic.setText(sb.toString());break;// 溫度感應器case Sensor.TYPE_AMBIENT_TEMPERATURE:sb = new StringBuilder();sb.append("當前溫度為:");sb.append(values[0]);etTemerature.setText(sb.toString());break;// 光感應器case Sensor.TYPE_LIGHT:sb = new StringBuilder();sb.append("當前光的強度為:");sb.append(values[0]);etLight.setText(sb.toString());break;// 壓力感應器case Sensor.TYPE_PRESSURE:sb = new StringBuilder();sb.append("當前壓力為:");sb.append(values[0]);etPressure.setText(sb.toString());break;default:break;}}// 當感應器精度改變時回調該方法@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}}


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.