網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了Android傳感器簡(jiǎn)單使用的具體代碼,供大家參考,具體內(nèi)容如下
1. SensorManager類
SensorManager類用來管理各個(gè)傳感器:通過SensorManager創(chuàng)建實(shí)例,并用getSystemService(SENSOR_SERVICE)獲取傳感器服務(wù)。
使用其getSensorList()方法,可以獲取所有可用的傳感器該方法返回一個(gè)List<Sensor>,即Sensor對(duì)象的列表。
注意:當(dāng)不使用或Activity暫停的時(shí)候,要關(guān)閉感應(yīng)器:屏幕關(guān)閉時(shí),系統(tǒng)不會(huì)自動(dòng)關(guān)閉感應(yīng)器,這會(huì)導(dǎo)致耗電增加,關(guān)閉的方法,就是解除對(duì)傳感器的監(jiān)聽。
2. Sensor類
Sensor實(shí)例對(duì)應(yīng)一個(gè)具體的傳感器,通過判斷Sensor的類型,來處理器傳感器信息,類型如下:
方向傳感器(Orientation sensor):SENSOR_TYPE_ORIENTATION
加速感應(yīng)器(Accelerometer sensor):SENSOR_TYPE_ACCELEROMETER
陀螺儀傳感器(Gyroscope sensor):SENSOR_TYPE_GYROSCOPE
磁場(chǎng)傳感器(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
相對(duì)濕度傳感器(Relative humidity sensor,Android 4.0引入)
近場(chǎng)通信(NFC)傳感器(Android 2.3引入),NFC和其他不一樣,具有讀寫功能。
3. SensorEventListener 監(jiān)聽器
使用SensorEventListener可以監(jiān)聽傳感器的各種事件,主要使用onSensorChanged()事件來獲取傳感器的信息。
onSensorChanged()事件的參數(shù)為SensorEvent對(duì)象,SensorEvent包含以下信息:
· 傳感器類型: Sensor sensor
· 傳感器數(shù)值精度:int accuracy
· 傳感器具體值:float[ ] values
通過訪問SensorEvent中的信息來獲取具體數(shù)值。
4. 使用傳感器的步驟
· 1. 定義SensorManager,并獲取SensorManager實(shí)例;
· 2. 定義Sensor,并指定傳感器;
· 3. 為定義的傳感器注冊(cè)事件監(jiān)聽事件:sensorManager.registerListener(三個(gè)參數(shù)),三個(gè)參數(shù)分別為:SensorEventListener、Sensor、更新速率;
· 4. 創(chuàng)建SensorEventListener監(jiān)聽器,獲取傳感器的值;
· 5. 退出應(yīng)用時(shí),應(yīng)注銷傳感器事件的監(jiān)聽:sensorManager.unregisterListener(Sensor sensor)。
☆☆☆A(yù)ndroid Studio實(shí)現(xiàn)在加速度傳感器的使用
1.打開Android Studio,新建工程后,在activity_main.xml中界添加一個(gè)按鈕和三個(gè)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ù)得到傳感器管理對(duì)象
? ? ? ? ? ? ? ? SensorManager sensorMgr = (SensorManager)
? ? ? ? ? ? ? ? ? ? ? ? getSystemService(SENSOR_SERVICE);
? ? ? ? ? ? ? ? //得到重力傳感器實(shí)例
? ? ? ? ? ? ? ? //TYPE_ACCELEROMETER 加速度傳感器(重力傳感器)類型。
? ? ? ? ? ? ? ? //TYPE_ALL 描述所有類型的傳感器。
? ? ? ? ? ? ? ? //TYPE_GYROSCOPE 陀螺儀傳感器類型
? ? ? ? ? ? ? ? //TYPE_LIGHT 光傳感器類型
? ? ? ? ? ? ? ? //TYPE_MAGNETIC_FIELD 恒定磁場(chǎng)傳感器類型。
? ? ? ? ? ? ? ? //TYPE_ORIENTATION 方向傳感器類型。
? ? ? ? ? ? ? ? //TYPE_PRESSURE 描述一個(gè)恒定的壓力傳感器類型
? ? ? ? ? ? ? ? //TYPE_PROXIMITY 常量描述型接近傳感器
? ? ? ? ? ? ? ? //TYPE_TEMPERATURE 溫度傳感器類型描述
? ? ? ? ? ? ? ? final Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
? ? ? ? ? ? ? ? SensorEventListener lsn = new SensorEventListener() {
? ? ? ? ? ? ? ? ? ? @SuppressWarnings("deprecation")//表示不檢測(cè)過期的方法
? ? ? ? ? ? ? ? ? ? //傳感器獲取值改變時(shí)響應(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 );//手機(jī)水平放置,左右x值
? ? ? ? ? ? ? ? ? ? ? ? tv2.setText("y=" + y );//手機(jī)水平放置,前后y值
? ? ? ? ? ? ? ? ? ? ? ? tv3.setText("z=" + z );//手機(jī)豎直放置,上下z值
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? public void onAccuracyChanged(Sensor s, int accuracy) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? //注冊(cè)listener,第三個(gè)參數(shù)是檢測(cè)的精確度
? ? ? ? ? ? ? ? sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
運(yùn)行結(jié)果:
☆☆☆A(yù)ndroid Studio實(shí)現(xiàn)在光線傳感器的使用
1.打開Android Studio,新建工程后,在activity_main.xml中界添加一個(gè)按鈕和一個(gè)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="光照強(qiáng)度" ? ? ? ? ? ? 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("光照強(qiáng)度為:\n"+event.values[0]+"勒克斯");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? mSManager.registerListener(mSEListener,mSen,SensorManager.SENSOR_DELAY_NORMAL);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
運(yùn)行結(jié)果:
這就是傳感器的簡(jiǎn)單使用。
原文鏈接:https://blog.csdn.net/weixin_43468667/article/details/97617736
相關(guān)推薦
- 2022-03-16 ASP.NET?Core在Linux下為dotnet創(chuàng)建守護(hù)進(jìn)程_基礎(chǔ)應(yīng)用
- 2022-02-27 Remove untracked files, stash or commit any change
- 2022-03-12 在CentOS7中安裝和配置ssh_Linux
- 2022-11-13 ASP.NET?MVC使用Session會(huì)話保持表單狀態(tài)_實(shí)用技巧
- 2022-03-26 android獲取及監(jiān)聽手機(jī)網(wǎng)絡(luò)狀態(tài)_Android
- 2022-08-31 C語(yǔ)言實(shí)現(xiàn)交換排序算法(冒泡,快速排序)的示例代碼_C 語(yǔ)言
- 2022-09-26 Linux系統(tǒng)監(jiān)控(top,ps,netstat,kill命令),定時(shí)任務(wù),系統(tǒng)啟動(dòng)(系統(tǒng)的啟動(dòng)級(jí)別
- 2022-08-27 C#使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支