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

Android實(shí)現(xiàn)接近傳感器

 更新時(shí)間:2020年04月20日 09:35:55   作者:一點(diǎn)一滴的積累  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)接近傳感器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)接近傳感器的具體代碼,供大家參考,具體內(nèi)容如下

1.接近傳感器檢測(cè)物體與聽(tīng)筒(手機(jī))的距離,單位是厘米。

一些接近傳感器只能返回遠(yuǎn)和近兩個(gè)狀態(tài),如我的手機(jī)魅族E2只能識(shí)別到兩個(gè)距離:0CM(近距離)和5CM(遠(yuǎn)距離)
因此,接近傳感器將最大距離返回遠(yuǎn)狀態(tài),小于最大距離返回近狀態(tài)。
接近傳感器可用于接聽(tīng)電話時(shí)自動(dòng)關(guān)閉LCD屏幕以節(jié)省電量。

一些芯片集成了接近傳感器和光線傳感器兩者功能(魅族E2)。

2.代碼如下:

MainActivity.class

package com.example.sz.proximitytest;

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.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
 private static final String TAG = "MainActivity";
 private SensorManager mSensorManager=null;
 private Sensor mSensor=null;
 private TextView textView1=null;
 private TextView textView2=null;
 private TextView textView3=null;
 private Button button1=null;
 private Button button2=null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 textView1 = (TextView) findViewById(R.id.textView1);
 textView2 = (TextView) findViewById(R.id.textView2);
 textView3 = (TextView) findViewById(R.id.textView3);
 /*獲取系統(tǒng)服務(wù)(SENSOR_SERVICE)返回一個(gè)SensorManager對(duì)象*/
 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
 /*通過(guò)SensorManager獲取相應(yīng)的(接近傳感器)Sensor類型對(duì)象*/
 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
 /*注冊(cè)相應(yīng)的SensorService*/
 button1 = (Button) findViewById(R.id.button1);
 button1.setOnClickListener(new Button.OnClickListener() {

  @Override
  public void onClick(View arg0) {
  mSensorManager.registerListener(mSensorEventListener, mSensor
   , SensorManager.SENSOR_DELAY_NORMAL);
  }
 });
 /* 銷毀相應(yīng)的SensorService
  * 很關(guān)鍵的部分,注意,說(shuō)明文檔中提到,即使Activity不可見(jiàn)的時(shí)候,感應(yīng)器依然會(huì)繼續(xù)工作
  * 所以一定要關(guān)閉觸發(fā)器,否則將消耗用戶大量電量*/
 button2 = (Button) findViewById(R.id.button2);
 button2.setOnClickListener(new Button.OnClickListener() {

  @Override
  public void onClick(View v) {
  mSensorManager.unregisterListener(mSensorEventListener, mSensor);
  }
 });
 }

 /*聲明一個(gè)SensorEventListener對(duì)象用于偵聽(tīng)Sensor事件,并重載onSensorChanged方法*/
 private final SensorEventListener mSensorEventListener = new SensorEventListener() {

 @Override
 public void onSensorChanged(SensorEvent event) {
  Log.e(TAG, "onSensorChanged: -----0------"+event.values[0]);
  Log.e(TAG, "onSensorChanged: ------1-----"+event.values[1]);
  Log.e(TAG, "onSensorChanged: --------2---"+event.values[2]);




  if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
  /*接近傳感器檢測(cè)物體與聽(tīng)筒的距離,單位是厘米。*/
  //這里要注意,正常都是取第一位的值,但我碰到一個(gè)取第二位的
  float distance1 = event.values[0];
  float distance2 = event.values[1];
  float distance3 = event.values[2];
  textView1.setText("[0]距離:"+String.valueOf(distance1) + "cm");
  textView2.setText("[1]距離:"+String.valueOf(distance2) + "cm");
  textView3.setText("[2]距離:"+String.valueOf(distance3) + "cm");
  }
 }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {

 }
 };


}

activity_main.xml

<?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:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:text="打開(kāi)" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:text="關(guān)閉" />
</LinearLayout>

源碼下載:Android接近傳感器

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

相關(guān)文章

  • Android Studio獲取配置資源與第三方包信息的方法

    Android Studio獲取配置資源與第三方包信息的方法

    在 Android 開(kāi)發(fā)中,我們經(jīng)常需要從資源文件中獲取顏色、字符串、數(shù)值等配置信息,以及獲取應(yīng)用的包信息和第三方依賴信息,下面詳細(xì)介紹這些操作的方法,需要的朋友可以參考下
    2025-04-04
  • Android權(quán)限機(jī)制帶來(lái)的一些安全問(wèn)題介紹

    Android權(quán)限機(jī)制帶來(lái)的一些安全問(wèn)題介紹

    這篇文章主要介紹了Android權(quán)限機(jī)制帶來(lái)的一些安全問(wèn)題介紹,本文講解了權(quán)限機(jī)制的缺陷和不足、樹(shù)立權(quán)限意識(shí)、越過(guò)權(quán)限機(jī)制等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Android自定義對(duì)話框Dialog

    Android自定義對(duì)話框Dialog

    這篇文章主要為大家詳細(xì)介紹了Android自定義對(duì)話框Dialog的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android編程實(shí)現(xiàn)微信分享信息的方法

    Android編程實(shí)現(xiàn)微信分享信息的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)微信分享信息的方法,實(shí)例分析了Android官方demo示例,講述了Android微信分享功能的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-10-10
  • Android編程自定義菜單實(shí)現(xiàn)方法詳解

    Android編程自定義菜單實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Android編程自定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android自定義菜單的布局、動(dòng)畫及功能相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-02-02
  • Flutter 局部路由實(shí)現(xiàn)詳解

    Flutter 局部路由實(shí)現(xiàn)詳解

    這篇文章主要介紹了Flutter 局部路由實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Android 屏幕切換監(jiān)聽(tīng)的實(shí)例代碼

    Android 屏幕切換監(jiān)聽(tīng)的實(shí)例代碼

    我試著在屏幕切換時(shí),使View顯示在不同的位置,在網(wǎng)上搜索了一些資料,自己做了一段時(shí)間,終于完成了功能,今天小編給大家分享android 屏幕切換監(jiān)聽(tīng)的實(shí)例代碼,需要的的朋友參考下吧
    2017-01-01
  • Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕

    Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕的具體代碼,感興趣的小伙伴們可以參考一下
    2016-05-05
  • flutter實(shí)現(xiàn)倒計(jì)時(shí)加載頁(yè)面

    flutter實(shí)現(xiàn)倒計(jì)時(shí)加載頁(yè)面

    這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)倒計(jì)時(shí)加載頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android應(yīng)用關(guān)閉的情況以及識(shí)別方法詳解

    Android應(yīng)用關(guān)閉的情況以及識(shí)別方法詳解

    對(duì)于現(xiàn)在的安卓手機(jī)而言,很多功能都是在逐步完善的,這篇文章主要給大家介紹了關(guān)于Android應(yīng)用關(guān)閉的情況以及識(shí)別的相關(guān)資料,文章通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評(píng)論

延川县| 黄冈市| 长沙县| 江口县| 藁城市| 邵阳市| 繁峙县| 长沙县| 芷江| 三穗县| 涞源县| 紫金县| 丰镇市| 马边| 邓州市| 陇西县| 新河县| 明水县| 吐鲁番市| 渭南市| 乌苏市| 满城县| 类乌齐县| 平谷区| 大理市| 云阳县| 溧阳市| 绥棱县| 临西县| 延长县| 武城县| 乐亭县| 裕民县| 崇州市| 安化县| 阜阳市| 鄂伦春自治旗| 陇南市| 老河口市| 乌拉特中旗| 文成县|