asp网站程序下载,电商网站建设的意义,艺术风格网站,什么叫品牌vi设计本文实例为大家分享了Android实现接近传感器的具体代码#xff0c;供大家参考#xff0c;具体内容如下1.接近传感器检测物体与听筒(手机)的距离#xff0c;单位是厘米。一些接近传感器只能返回远和近两个状态#xff0c;如我的手机魅族E2只能识别到两个距离#xff1a;0CM…本文实例为大家分享了Android实现接近传感器的具体代码供大家参考具体内容如下1.接近传感器检测物体与听筒(手机)的距离单位是厘米。一些接近传感器只能返回远和近两个状态如我的手机魅族E2只能识别到两个距离0CM(近距离)和5CM(远距离)因此接近传感器将最大距离返回远状态小于最大距离返回近状态。接近传感器可用于接听电话时自动关闭LCD屏幕以节省电量。一些芯片集成了接近传感器和光线传感器两者功能(魅族E2)。2.代码如下MainActivity.classpackage 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 mSensorManagernull;private Sensor mSensornull;private TextView textView1null;private TextView textView2null;private TextView textView3null;private Button button1null;private Button button2null;Overrideprotected 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() {Overridepublic 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() {Overridepublic void onClick(View v) {mSensorManager.unregisterListener(mSensorEventListener, mSensor);}});}/*声明一个SensorEventListener对象用于侦听Sensor事件并重载onSensorChanged方法*/private final SensorEventListener mSensorEventListener new SensorEventListener() {Overridepublic 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);}}Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}};}activity_main.xmlxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:orientationverticaltools:context.MainActivityandroid:idid/textView1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textHello World! /android:idid/button1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop20dpandroid:text打开 /android:idid/button2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop20dpandroid:text关闭 /以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持编程圈。