当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现接近传感器

Android实现接近传感器

2020年05月11日  | 移动技术网IT编程  | 我要评论

青出于蓝造句,土加布,邯郸大学生

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

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

源码下载:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网