最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android傳感器的簡單使用方法

 更新時間:2022年09月20日 10:23:48   作者:明昕1024  
這篇文章主要為大家詳細(xì)介紹了Android傳感器的簡單使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android傳感器簡單使用的具體代碼,供大家參考,具體內(nèi)容如下

1. SensorManager類

SensorManager類用來管理各個傳感器:通過SensorManager創(chuàng)建實(shí)例,并用getSystemService(SENSOR_SERVICE)獲取傳感器服務(wù)。
使用其getSensorList()方法,可以獲取所有可用的傳感器該方法返回一個List<Sensor>,即Sensor對象的列表。
注意:當(dāng)不使用或Activity暫停的時候,要關(guān)閉感應(yīng)器:屏幕關(guān)閉時,系統(tǒng)不會自動關(guān)閉感應(yīng)器,這會導(dǎo)致耗電增加,關(guān)閉的方法,就是解除對傳感器的監(jiān)聽。

2. Sensor類

Sensor實(shí)例對應(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實(shí)例;
· 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實(shí)現(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);
? ? ? ? ? ? ? ? //得到重力傳感器實(shí)例
? ? ? ? ? ? ? ? //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 );//手機(jī)水平放置,左右x值
? ? ? ? ? ? ? ? ? ? ? ? tv2.setText("y=" + y );//手機(jī)水平放置,前后y值
? ? ? ? ? ? ? ? ? ? ? ? tv3.setText("z=" + z );//手機(jī)豎直放置,上下z值
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? public void onAccuracyChanged(Sensor s, int accuracy) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? //注冊listener,第三個參數(shù)是檢測的精確度
? ? ? ? ? ? ? ? sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

運(yùn)行結(jié)果:

☆☆☆A(yù)ndroid Studio實(shí)現(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="光照強(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é)果:

這就是傳感器的簡單使用。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android編程實(shí)現(xiàn)扭曲圖像的繪制功能示例

    Android編程實(shí)現(xiàn)扭曲圖像的繪制功能示例

    這篇文章主要介紹了Android編程實(shí)現(xiàn)扭曲圖像的繪制功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android圖形扭曲的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼

    WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼

    這篇文章主要介紹了WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 詳解SwipeListView框架實(shí)現(xiàn)微信\QQ滑動刪除效果

    詳解SwipeListView框架實(shí)現(xiàn)微信\QQ滑動刪除效果

    這篇文章主要為大家詳細(xì)介紹了SwipeListView框架實(shí)現(xiàn)微信\QQ滑動刪除效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android?Flutter在點(diǎn)擊事件上添加動畫效果實(shí)現(xiàn)全過程

    Android?Flutter在點(diǎn)擊事件上添加動畫效果實(shí)現(xiàn)全過程

    這篇文章主要給大家介紹了關(guān)于Android?Flutter在點(diǎn)擊事件上添加動畫效果實(shí)現(xiàn)的相關(guān)資料,通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)Android具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-03-03
  • Flutter實(shí)現(xiàn)旋轉(zhuǎn)掃描效果

    Flutter實(shí)現(xiàn)旋轉(zhuǎn)掃描效果

    這篇文章主要介紹了通過Flutter RotationTransition實(shí)現(xiàn)雷達(dá)旋轉(zhuǎn)掃描的效果,文中的示例代碼講解詳細(xì),感興趣的同學(xué)可以動手試一試
    2022-01-01
  • Android實(shí)現(xiàn)精美的聊天界面

    Android實(shí)現(xiàn)精美的聊天界面

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)精美的聊天界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android仿QQ未讀消息--紅點(diǎn)拖拽刪除【源代碼】

    Android仿QQ未讀消息--紅點(diǎn)拖拽刪除【源代碼】

    本文Demo是一款仿qq未讀消息拖拽刪除的例子,繼承RelativeLayout的WaterDrop實(shí)現(xiàn)了圓形圖標(biāo)功能;繼承ImageView的CircleImageView圓形圖片功能。效果非常不錯,很適合有圓形設(shè)計(jì)的朋友參考
    2017-04-04
  • Android Studio一直處于Building的兩種解決方法

    Android Studio一直處于Building的兩種解決方法

    很多朋友都遇到過打開別人的項(xiàng)目一直處于Building‘XXX’Gradle project info的情況。下面小編給大家?guī)砹薃ndroid Studio一直處于Building的解決方法,感興趣的朋友一起看看吧
    2018-08-08
  • 深入android中The connection to adb is down的問題以及解決方法

    深入android中The connection to adb is 

    本篇文章是對android中The connection to adb is down的問題以及解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 基于Android實(shí)現(xiàn)煙花效果

    基于Android實(shí)現(xiàn)煙花效果

    這篇文章文章我們將介紹如何通過Canvas 2D坐標(biāo)系實(shí)現(xiàn)粒子效果,文中通過代碼示例和圖文結(jié)合介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動手嘗試一下
    2023-11-11

最新評論

宜昌市| 社会| 大宁县| 周口市| 朔州市| 右玉县| 桃江县| 福海县| 准格尔旗| 永康市| 尚义县| 阜宁县| 遵化市| 吴川市| 贵阳市| 樟树市| 嘉禾县| 天台县| 东宁县| 鲁甸县| 鹿泉市| 武城县| 吴川市| 金坛市| 尼勒克县| 英超| 郓城县| 东乌珠穆沁旗| 武隆县| 军事| 新乐市| 鄂伦春自治旗| 泌阳县| 麟游县| 彰武县| 浠水县| 太康县| 黄大仙区| 同仁县| 乌兰浩特市| 梁河县|