所以可以透過本程式可以知道手機上擁有哪些感測器和感測器的值,甚至還可以知道這些感測的品牌和型號ㄟ....有沒有給他很方便阿!
程式執行畫面如下:
我的智慧型手機型號是:HTC Rhyme音韻機(點我查看資訊)
執行程式之後,我可以知道我手機內建有:加速度、磁力感測、姿態感測、遠近感測、光學感測、重力感測、線性加速度感測、旋轉向量感測
程式碼如下:
package com.cavedu; import java.util.List; import android.hardware.SensorManager; import android.hardware.Sensor; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.widget.TextView; public class SensorListActivity extends Activity { private SensorManager m_sensorManager; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); m_sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//取得手機的感測器資訊 List<Sensor> availableSensor = m_sensorManager.getSensorList(Sensor.TYPE_ALL);//取得手機感測器清單 TextView sensorList = (TextView)findViewById(R.id.sensor_list_text);//宣告文字方塊,並取得方塊編號 String outputString = ""; for (int i =0; i<availableSensor.size(); i++ ){ Sensor thisSensor = availableSensor.get(i); outputString = thisSensor.getName(); //取得裝置名稱 outputString = printType(thisSensor.getType()); //取得型態 outputString = ", "; outputString = thisSensor.getPower(); //取得功耗 outputString = "毫安培\n"; outputString = thisSensor.getVendor(); //取得供應商 outputString = thisSensor.getMaximumRange(); //資料範圍 outputString = "\n"; } sensorList.setText(outputString);//顯示字串 }//onCreate //處理裝置型態的回傳值轉換成文字 private String printType(int i){ switch(i){ case 1: return "(加速度感應器)"; case 2: return "(磁力感應器)"; case 3: return "(姿態感應器)"; case 4: return "(陀螺儀感應器)"; case 5: return "(光感應器)"; case 6: return "(壓力感應器)"; case 7: return "(溫度感應器)"; case 8: return "(遠近感應器)"; }//switch return ""; } }
附上整個專案內容: | |
檔案名稱(檔案大小):SensorList.rar (51 KB) | |
檔案空間:MEGA | |
檔案連結:點我下載 | |
下載金鑰:LyZnWlUw16yK0HO-qptjskzUOTUo94ri4bWJrsX4-lQ |