本文实例为大家分享了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

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">

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!" />

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:text="打开" />

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:text="关闭" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐