Android实现接近传感器

 更新时间:2020年04月20日 09:35:55   作者:一点一滴的积累  
这篇文章主要为大家详细介绍了Android实现接近传感器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

源码下载:Android接近传感器

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

相关文章

  • Android购物车项目快速开发

    Android购物车项目快速开发

    这篇文章主要为大家详细介绍了Android购物车项目快速开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android开发笔记之:在ImageView上绘制圆环的实现方法

    Android开发笔记之:在ImageView上绘制圆环的实现方法

    本篇文章是对Android中在ImageView上绘制圆环的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 详解okhttp3 请求头不能为中文的坑

    详解okhttp3 请求头不能为中文的坑

    这篇文章主要介绍了详解okhttp3 请求头不能为中文的坑,非常具有实用价值,需要的朋友可以参考下
    2017-09-09
  • 基于Flutter实现动态高斯模糊的流程步骤

    基于Flutter实现动态高斯模糊的流程步骤

    一个App加上高斯模糊会形成一种高级的感觉,本文将介绍如何制作一个根据背景内容来动态高斯模糊,文中有详细的代码实现步骤,代码示例讲解的非常详细,具有一定的参考价值,需要的朋友可以参考下
    2023-11-11
  • Android中ImageView使用网络图片资源的方法

    Android中ImageView使用网络图片资源的方法

    这篇文章主要介绍了Android中ImageView使用网络图片资源的方法,较为详细的分析了ImageView调用网络图片的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android Force Close 出现的异常原因分析及解决方法

    Android Force Close 出现的异常原因分析及解决方法

    本文给大家讲解Android Force Close 出现的异常原因分析及解决方法,forceclose意为强行关闭,当前应用程序发生了冲突。对android force close异常分析感兴趣的朋友一起通过本文学习吧
    2016-08-08
  • Android控件Spinner的使用方法(1)

    Android控件Spinner的使用方法(1)

    这篇文章主要为大家详细介绍了Android控件Spinner的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android RecyclerView添加上拉加载更多功能

    Android RecyclerView添加上拉加载更多功能

    这篇文章主要为大家详细介绍了Android RecyclerView如何添加上拉加载更多功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Android中Java instanceof关键字全面解析

    Android中Java instanceof关键字全面解析

    instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口、抽象类、父类)的实例.这篇文章主要介绍了Android中Java instanceof关键字全面解析的相关资料,需要的朋友可以参考下
    2016-07-07
  • Android 自定义来电秀实现总结

    Android 自定义来电秀实现总结

    这篇文章主要为大家介绍了Android 自定义来电秀实现总结示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01

最新评论