Android如何实现接近传感器

  介绍

小编这次要给大家分享的是Android如何实现接近传感器,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

1。接近传感器检测物体与听筒(手机)的距离,单位是厘米只

一些接近传感器只能返回远和近两个状态,如我的手机魅族E2只能识别到两个距离:0厘米(近距离)和5厘米(远距离)
因此,接近传感器将最大距离返回远状态,小于最大距离返回近状态。
接近传感器可用于接听电话时自动关闭液晶屏幕以节省电量。

一些芯片集成了接近传感器和光线传感器两者功能(魅族E2)。

2。代码如下:

MainActivity。类

包com.example.sz.proximitytest;      进口android.hardware.Sensor;   进口android.hardware.SensorEvent;   进口android.hardware.SensorEventListener;   进口android.hardware.SensorManager;   进口android.support.v7.app.AppCompatActivity;   进口android.os.Bundle;   进口android.util.Log;   进口android.view.Menu;   进口android.view.View;   进口android.widget.Button;   进口android.widget.TextView;      公开课MainActivity延伸AppCompatActivity {   私有静态最终字符串标签=癕ainActivity";   私人SensorManager mSensorManager=零;   私人传感器mSensor=零;   私人TextView textView1=零;   私人TextView textView2=零;   私人TextView textView3=零;   私人按钮button1=零;   私人按钮button2=零;      @Override   保护空白> & lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt; LinearLayout xmlns: android=?;   xmlns:应用=癶ttp://schemas.android.com/apk/res-auto"   xmlns:工具=癶ttp://schemas.android.com/tools"   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   android:重力=癱enter"   android:取向=皏ertical"   工具:上下文=?MainActivity"比;      & lt; TextView   android: id=癅 + id/textView1"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:文本=癏ello World !“/比;      & lt;按钮   android: id=癅 + id/button1"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_marginTop=?0 dp"   android:文本=按蚩?比;      & lt;按钮   android: id=癅 + id/button2"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_marginTop=?0 dp"   android:文本=肮乇?/比;   & lt;/LinearLayout>

看完这篇关于Android如何实现接近传感器的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

Android如何实现接近传感器