傳感器是一種檢測裝置,能夠感受被測量的信息,並能將檢測和感受到的信息按一定規律變換成電信號或其它所需形式的信息輸出
Android操作系統中內置了很多的傳感器(物理裝置),能夠探測、感受外界的信號、物理條件,並將得到的信息傳遞給其它的裝置。
例如在部分游戲或軟件可以自動識別屏幕的橫豎屏來改變屏幕顯示的布局
下面是Android支持的幾種傳感器:
加速傳感器 Sensor.TYPE_ACCELEROMETER
陀螺儀傳感器 Sensor.TYPE_GYROSCOPE
環境光儀傳感器 Sensor.TYPE_LIGHT
電磁場傳感器 Sensor.TYPE_MAGNETIC_FIELD
方向傳感器 Sensor.TYPE_ORIENTATION:
壓力傳感器 Sensor.TYPE_PRESSURE:
距離傳感器 Sensor.TYPE_PROXIMITY:
溫度傳感器 Sensor.TYPE_TEMPERATURE:
運行截圖:
程序結構:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.example.asus.gary_01.MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="傳感器操作!" android:textSize="10pt" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="獲取手機傳感器信息"/> <TextView android:id="@+id/textView" android:scrollbars="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="8pt"/> </LinearLayout>

package com.example.asus.gary_01; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { private TextView tx1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt1 = (Button)findViewById(R.id.button); tx1=(TextView)findViewById(R.id.textView); //從系統獲得傳感器管理器 final SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str; //從傳感器管理器中獲得全部的傳感器列表 List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL); int i; //給ViewText添加滾動條 tx1.setMovementMethod(ScrollingMovementMethod.getInstance()); //顯示有多少個傳感器 tx1.setText("經檢測該手機有"+allSensors.size()+"個傳感器,它們分別是:"); Sensor s; //顯示每個傳感器的具體信息 for(i=0;i<allSensors.size();i++) { s=allSensors.get(i); str="設備名稱:"+s.getName(); switch(s.getType()) { //加速傳感器 Sensor.TYPE_ACCELEROMETER case Sensor.TYPE_ACCELEROMETER: tx1.setText(tx1.getText()+"\n"+i+"加速傳感器accelerometer:\n"+str); break; //陀螺儀傳感器 Sensor.TYPE_GYROSCOPE case Sensor.TYPE_GYROSCOPE: tx1.setText(tx1.getText()+"\n"+i+"陀螺儀傳感器gyroscope:\n"+str); break; //環境光儀傳感器 Sensor.TYPE_LIGHT case Sensor.TYPE_LIGHT: tx1.setText(tx1.getText()+"\n"+i+"環境光儀傳感器light:\n"+str); break; //電磁場傳感器 Sensor.TYPE_MAGNETIC_FIELD case Sensor.TYPE_MAGNETIC_FIELD: tx1.setText(tx1.getText()+"\n"+i+"電磁場傳感器magnetic:\n"+str); break; //方向傳感器 Sensor.TYPE_ORIENTATION: case Sensor.TYPE_ORIENTATION: tx1.setText(tx1.getText()+"\n"+i+"方向傳感器orientation:\n"+str); break; //壓力傳感器 Sensor.TYPE_PRESSURE: case Sensor.TYPE_PRESSURE: tx1.setText(tx1.getText()+"\n"+i+"壓力傳感器pressure:\n"+str); break; //距離傳感器 Sensor.TYPE_PROXIMITY: case Sensor.TYPE_PROXIMITY: tx1.setText(tx1.getText()+"\n"+i+"距離傳感器proximity:\n"+str); break; //溫度傳感器 Sensor.TYPE_TEMPERATURE: case Sensor.TYPE_TEMPERATURE: tx1.setText(tx1.getText()+"\n"+i+"溫度傳感器temperature:\n"+str); break; default: tx1.setText(tx1.getText()+"\n"+i+"未知傳感器:\n"+str); break; } } } }); } }
一、界面布局
兩個TextView,一個Button,下方的TextView支持滾動操作
點擊Button,獲得手機傳感器並顯示在下方TextView上
給textview添加滾動條:傳送門
xml代碼:
//設置滾動條的方向
android:scrollbars="vertical"
java中設置
tx1=(TextView) findViewById(R.id.tv1);
//設置滾動方式
tx1.setMovementMethod(ScrollingMovementMethod.getInstance());
二、實現程序
//從系統獲得傳感器管理器
final SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
//從傳感器管理器中獲得全部的傳感器列表
List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str; //從傳感器管理器中獲得全部的傳感器列表 List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL); int i; //給ViewText添加滾動條 tx1.setMovementMethod(ScrollingMovementMethod.getInstance()); //顯示有多少個傳感器 tx1.setText("經檢測該手機有"+allSensors.size()+"個傳感器,它們分別是:"); Sensor s; //顯示每個傳感器的具體信息 for(i=0;i<allSensors.size();i++) { s=allSensors.get(i); str="設備名稱:"+s.getName(); switch(s.getType()) { //加速傳感器 Sensor.TYPE_ACCELEROMETER case Sensor.TYPE_ACCELEROMETER: tx1.setText(tx1.getText()+"\n"+i+"加速傳感器accelerometer:\n"+str); break; //陀螺儀傳感器 Sensor.TYPE_GYROSCOPE case Sensor.TYPE_GYROSCOPE: tx1.setText(tx1.getText()+"\n"+i+"陀螺儀傳感器gyroscope:\n"+str); break; //環境光儀傳感器 Sensor.TYPE_LIGHT case Sensor.TYPE_LIGHT: tx1.setText(tx1.getText()+"\n"+i+"環境光儀傳感器light:\n"+str); break; //電磁場傳感器 Sensor.TYPE_MAGNETIC_FIELD case Sensor.TYPE_MAGNETIC_FIELD: tx1.setText(tx1.getText()+"\n"+i+"電磁場傳感器magnetic:\n"+str); break; //方向傳感器 Sensor.TYPE_ORIENTATION: case Sensor.TYPE_ORIENTATION: tx1.setText(tx1.getText()+"\n"+i+"方向傳感器orientation:\n"+str); break; //壓力傳感器 Sensor.TYPE_PRESSURE: case Sensor.TYPE_PRESSURE: tx1.setText(tx1.getText()+"\n"+i+"壓力傳感器pressure:\n"+str); break; //距離傳感器 Sensor.TYPE_PROXIMITY: case Sensor.TYPE_PROXIMITY: tx1.setText(tx1.getText()+"\n"+i+"距離傳感器proximity:\n"+str); break; //溫度傳感器 Sensor.TYPE_TEMPERATURE: case Sensor.TYPE_TEMPERATURE: tx1.setText(tx1.getText()+"\n"+i+"溫度傳感器temperature:\n"+str); break; default: tx1.setText(tx1.getText()+"\n"+i+"未知傳感器:\n"+str); break; } } } });