亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

本文實(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: 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='打開(kāi)' /> <Button android: 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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 国产精品美女在线观看 | 国产福利一区二区三区四区 | 免费看片网址 | 成人午夜免费剧场 | 亚洲第一区在线观看 | 最新国产在线视频 | 成人精品一区二区户外勾搭野战 | 国产女人久久精品 | 国产小视频网站 | 三级黄色片在线免费观看 | 精品国产97在线观看 | 高清国产天干天干天干不卡顿 | 久久久久久一级毛片免费野外 | 欧美日韩亚洲另类人人澡 | 国产51页 | 国产黄色在线看 | 国产精品视频自拍 | 最新毛片网 | 亚洲国产高清精品线久久 | 亚洲黄色a | 国产精品久久亚洲一区二区 | www.青青草 | 国精品日韩欧美一区二区三区 | 亚洲精品不卡久久久久久 | 久久精品在线 | 黄色免费小视频 | 久久99国产这里有精品视 | 小黄片毛片 | 小泽玛利亚在线精品一区二区 | 在线青草| 成人在线激情视频 | 制服丝袜第一页在线观看 | 2018高清国产一道国产 | 久久国产精品自线拍免费 | 麻豆传媒小视频 | 91福利国产在线观看香蕉 | 国产剧情福利 | 欧美黑人两根巨大挤入 | 国产成人禁片免费观看视频 | 久热精品视频在线 | 亚洲色图综合区 |