国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android實現接近傳感器

瀏覽:6日期:2022-09-24 17:10:39

本文實例為大家分享了Android實現接近傳感器的具體代碼,供大家參考,具體內容如下

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

一些接近傳感器只能返回遠和近兩個狀態,如我的手機魅族E2只能識別到兩個距離:0CM(近距離)和5CM(遠距離) 因此,接近傳感器將最大距離返回遠狀態,小于最大距離返回近狀態。 接近傳感器可用于接聽電話時自動關閉LCD屏幕以節省電量。

一些芯片集成了接近傳感器和光線傳感器兩者功能(魅族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); /*獲取系統服務(SENSOR_SERVICE)返回一個SensorManager對象*/ mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); /*通過SensorManager獲取相應的(接近傳感器)Sensor類型對象*/ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); /*注冊相應的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); } }); /* 銷毀相應的SensorService * 很關鍵的部分,注意,說明文檔中提到,即使Activity不可見的時候,感應器依然會繼續工作 * 所以一定要關閉觸發器,否則將消耗用戶大量電量*/ button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { mSensorManager.unregisterListener(mSensorEventListener, mSensor); } }); } /*聲明一個SensorEventListener對象用于偵聽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) { /*接近傳感器檢測物體與聽筒的距離,單位是厘米。*/ //這里要注意,正常都是取第一位的值,但我碰到一個取第二位的 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: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='Hello World!' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='20dp' android:text='打開' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='20dp' android:text='關閉' /></LinearLayout>

源碼下載:Android接近傳感器

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 精品欧美成人高清在线观看2021 | 女bbbbxxxx毛片视频0 | 欧美精品一区二区三区免费观看 | 一级美国片免费看 | 国产精品黄在线观看观看 | 男女性关系视频免费观看软件 | 久草久草视频 | 日韩欧美视频在线播放 | 精品国产欧美一区二区最新 | 亚洲另类激情综合偷自拍图 | 久99re视频9在线观看 | 男人的天堂欧美精品色偷偷 | 欧美大尺度免费一级特黄 | 日本三级网站 | 色噜噜国产精品视频一区二区 | 国产视频高清在线 | 亚洲成a人片在线观看中文!!! | 精品72久久久久久久中文字幕 | 欧美三级黄色大片 | 91亚洲精品久久91 | 国内主播福利视频在线观看 | 在线91精品亚洲网站精品成人 | 男女超猛烈啪啦啦的免费视频 | 国产大片线上免费观看 | 国内一区二区 | 国产福利一区二区三区 | 狠狠色狠狠色综合日日32 | 日韩午夜免费视频 | 国产免费一区二区三区在线观看 | 久久www免费人成看国产片 | a站在线观看 | 手机看片国产免费久久网 | 欧美xxxxb| 国产成人精品无缓存在线播放 | www.黄色免费网站 | 在线视频亚洲 | 亚洲黄色免费在线观看 | 麻豆国产视频 | 欧美激情亚洲 | 亚洲国产一区二区a毛片日本 | 中文字幕 亚洲精品 第1页 |