Android傳感器的簡單使用方法
本文實例為大家分享了Android傳感器簡單使用的具體代碼,供大家參考,具體內(nèi)容如下
1. SensorManager類
SensorManager類用來管理各個傳感器:通過SensorManager創(chuàng)建實例,并用getSystemService(SENSOR_SERVICE)獲取傳感器服務(wù)。
使用其getSensorList()方法,可以獲取所有可用的傳感器該方法返回一個List<Sensor>,即Sensor對象的列表。
注意:當不使用或Activity暫停的時候,要關(guān)閉感應(yīng)器:屏幕關(guān)閉時,系統(tǒng)不會自動關(guān)閉感應(yīng)器,這會導致耗電增加,關(guān)閉的方法,就是解除對傳感器的監(jiān)聽。
2. Sensor類
Sensor實例對應(yīng)一個具體的傳感器,通過判斷Sensor的類型,來處理器傳感器信息,類型如下:
方向傳感器(Orientation sensor):SENSOR_TYPE_ORIENTATION
加速感應(yīng)器(Accelerometer sensor):SENSOR_TYPE_ACCELEROMETER
陀螺儀傳感器(Gyroscope sensor):SENSOR_TYPE_GYROSCOPE
磁場傳感器(Magnetic field sensor):SENSOR_TYPE_MAGNETIC_FIELD
接近(距離)感應(yīng)器(Proximity sensor):SENSOR_TYPE_PROXIMITY
光線傳感器(Light sensor):SENSOR_TYPE_LIGHT
氣壓傳感器(Pressure sensor):SENSOR_TYPE_PRESSURE
溫度傳感器(Temperature sensor): SENSOR_TYPE_TEMPERATURE
重力感應(yīng)器(Gravity sensor,Android 2.3引入):SENSOR_TYPE_GRAVITY
線性加速感應(yīng)器(Linear acceleration sensor ,Android 2.3引入):SENSOR_TYPE_LINEAR_ACCELERATION
旋轉(zhuǎn)矢量傳感器(Rotation vector sensor,Android 2.3引入): SENSOR_TYPE_ROTATION_VECTOR
相對濕度傳感器(Relative humidity sensor,Android 4.0引入)
近場通信(NFC)傳感器(Android 2.3引入),NFC和其他不一樣,具有讀寫功能。
3. SensorEventListener 監(jiān)聽器
使用SensorEventListener可以監(jiān)聽傳感器的各種事件,主要使用onSensorChanged()事件來獲取傳感器的信息。
onSensorChanged()事件的參數(shù)為SensorEvent對象,SensorEvent包含以下信息:
· 傳感器類型: Sensor sensor
· 傳感器數(shù)值精度:int accuracy
· 傳感器具體值:float[ ] values
通過訪問SensorEvent中的信息來獲取具體數(shù)值。
4. 使用傳感器的步驟
· 1. 定義SensorManager,并獲取SensorManager實例;
· 2. 定義Sensor,并指定傳感器;
· 3. 為定義的傳感器注冊事件監(jiān)聽事件:sensorManager.registerListener(三個參數(shù)),三個參數(shù)分別為:SensorEventListener、Sensor、更新速率;
· 4. 創(chuàng)建SensorEventListener監(jiān)聽器,獲取傳感器的值;
· 5. 退出應(yīng)用時,應(yīng)注銷傳感器事件的監(jiān)聽:sensorManager.unregisterListener(Sensor sensor)。
☆☆☆A(yù)ndroid Studio實現(xiàn)在加速度傳感器的使用
1.打開Android Studio,新建工程后,在activity_main.xml中界添加一個按鈕和三個TextView。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/activity_main" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? 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="lession.example.com.androidlession616.MainActivity"> ? ?? ? ? <LinearLayout ? ? ? ? android:orientation="vertical" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent"> ? ? ? ?? ? ? ? ? <Button ? ? ? ? ? ? android:text="使用三軸加速度感應(yīng)器(重力)" ? ? ? ? ? ? android:layout_width="356dp" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/button" ? ? ? ? ? ? android:textColor="@android:color/holo_red_dark" ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ?? ? ? ? ? <TextView ? ? ? ? ? ? android:text="TextView" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_below="@+id/button" ? ? ? ? ? ? android:id="@+id/textView" ? ? ? ? ? ? android:textColor="@android:color/holo_green_dark" ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ?? ? ? ? ? <TextView ? ? ? ? ? ? android:text="TextView" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/textView2" ? ? ? ? ? ? android:textColor="@android:color/holo_green_dark" ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ?? ? ? ? ? <TextView ? ? ? ? ? ? android:text="TextView" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/textView3" ? ? ? ? ? ? android:textColor="@android:color/holo_green_dark" ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ?? ? ? </LinearLayout> ? ?? </RelativeLayout>
2.在MainActivity中,編寫代碼。
package lession.example.com.androidlession616; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { ? ? private TextView tv1,tv2,tv3; ? ? private float x, y, z; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? Button bt = (Button) findViewById(R.id.button); ? ? ? ? tv1 = (TextView) findViewById(R.id.textView); ? ? ? ? tv2 = (TextView) findViewById(R.id.textView2); ? ? ? ? tv3 = (TextView) findViewById(R.id.textView3); ? ? ? ? bt.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? //通過服務(wù)得到傳感器管理對象 ? ? ? ? ? ? ? ? SensorManager sensorMgr = (SensorManager) ? ? ? ? ? ? ? ? ? ? ? ? getSystemService(SENSOR_SERVICE); ? ? ? ? ? ? ? ? //得到重力傳感器實例 ? ? ? ? ? ? ? ? //TYPE_ACCELEROMETER 加速度傳感器(重力傳感器)類型。 ? ? ? ? ? ? ? ? //TYPE_ALL 描述所有類型的傳感器。 ? ? ? ? ? ? ? ? //TYPE_GYROSCOPE 陀螺儀傳感器類型 ? ? ? ? ? ? ? ? //TYPE_LIGHT 光傳感器類型 ? ? ? ? ? ? ? ? //TYPE_MAGNETIC_FIELD 恒定磁場傳感器類型。 ? ? ? ? ? ? ? ? //TYPE_ORIENTATION 方向傳感器類型。 ? ? ? ? ? ? ? ? //TYPE_PRESSURE 描述一個恒定的壓力傳感器類型 ? ? ? ? ? ? ? ? //TYPE_PROXIMITY 常量描述型接近傳感器 ? ? ? ? ? ? ? ? //TYPE_TEMPERATURE 溫度傳感器類型描述 ? ? ? ? ? ? ? ? final Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); ? ? ? ? ? ? ? ? SensorEventListener lsn = new SensorEventListener() { ? ? ? ? ? ? ? ? ? ? @SuppressWarnings("deprecation")//表示不檢測過期的方法 ? ? ? ? ? ? ? ? ? ? //傳感器獲取值改變時響應(yīng)此函數(shù) ? ? ? ? ? ? ? ? ? ? public void onSensorChanged(SensorEvent e) { ? ? ? ? ? ? ? ? ? ? ? ? x = e.values[0]; ? ? ? ? ? ? ? ? ? ? ? ? y = e.values[1]; ? ? ? ? ? ? ? ? ? ? ? ? z = e.values[2]; // ? ? ? ? ? ? ? ? ? ? ? ?x = e.values[SensorManager.DATA_X]; // ? ? ? ? ? ? ? ? ? ? ? ?y = e.values[SensorManager.DATA_Y]; // ? ? ? ? ? ? ? ? ? ? ? ?z = e.values[SensorManager.DATA_Z]; ? ? ? ? ? ? ? ? ? ? ? ? tv1.setText("x=" + x );//手機水平放置,左右x值 ? ? ? ? ? ? ? ? ? ? ? ? tv2.setText("y=" + y );//手機水平放置,前后y值 ? ? ? ? ? ? ? ? ? ? ? ? tv3.setText("z=" + z );//手機豎直放置,上下z值 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? public void onAccuracyChanged(Sensor s, int accuracy) { ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? ? ? //注冊listener,第三個參數(shù)是檢測的精確度 ? ? ? ? ? ? ? ? sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
運行結(jié)果:
☆☆☆A(yù)ndroid Studio實現(xiàn)在光線傳感器的使用
1.打開Android Studio,新建工程后,在activity_main.xml中界添加一個按鈕和一個TextView。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/activity_main" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? 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="lession.example.com.androidlession616_2.MainActivity"> ? ?? ? ? <LinearLayout ? ? ? ? android:orientation="vertical" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent"> ? ? ? ?? ? ? ? ? <Button ? ? ? ? ? ? android:text="光照強度" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/button" ? ? ? ? ? ? android:textColor="@android:color/holo_red_dark" ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ?? ? ? ? ? <TextView ? ? ? ? ? ? android:text="TextView" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/textView" ? ? ? ? ? ? android:textColor="@android:color/holo_green_dark" ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ?? ? ? </LinearLayout> ? ?? </RelativeLayout>
2.在MainActivity中,編寫代碼。
package lession.example.com.androidlession616_2; 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.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? final TextView tv = (TextView) findViewById(R.id.textView); ? ? ? ? Button bt = (Button) findViewById(R.id.button); ? ? ? ? bt.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? SensorManager mSManager = (SensorManager) ? ? ? ? ? ? ? ? ? ? ? ? getSystemService(Context.SENSOR_SERVICE); ? ? ? ? ? ? ? ? Sensor mSen = mSManager.getDefaultSensor(Sensor.TYPE_LIGHT); ? ? ? ? ? ? ? ? SensorEventListener mSEListener = new SensorEventListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onSensorChanged(SensorEvent event) { ? ? ? ? ? ? ? ? ? ? ? ? tv.setText("光照強度為:\n"+event.values[0]+"勒克斯"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) { ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? ? ? mSManager.registerListener(mSEListener,mSen,SensorManager.SENSOR_DELAY_NORMAL); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
運行結(jié)果:
這就是傳感器的簡單使用。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)Jetpack組件Lifecycle原理篇
這一篇文章來介紹Android?Jetpack架構(gòu)組件的Lifecycle;?Lifecycle用于幫助開發(fā)者管理Activity和Fragment?的生命周期,?由于Lifecycle是LiveData和ViewModel的基礎(chǔ);所以需要先學習它2022-08-08AndroidManifest.xml uses-feature功能詳解
這篇文章主要介紹了AndroidManifest.xml uses-feature功能,較為詳細的分析了Android屬性過濾操作的功能與相關(guān)技巧,需要的朋友可以參考下2016-10-10Android入門之在Activity之間穿梭的Intent
Intent可以用來啟動Activity(startActivity(Intent))、Serveice(startService(Intent))等組件,可以用來綁定Activity和Service以建立它們之間的通信(bindServiceConnaction(Intent,ServiceConnection,int)),可以作為Broadcast Intent發(fā)送給廣播接收器2021-10-10Android實現(xiàn)簡單底部導航欄 Android仿微信滑動切換效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)簡單底部導航欄,Android仿微信滑動切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08