本文实例为大家分享了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);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
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);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
mSensorManager.unregisterListener(mSensorEventListener, mSensor);
}
});
}
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: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="打开" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="关闭" />
</LinearLayout>
源码下载:Android接近传感器