Android实现接近传感器

所属分类: 软件编程 / Android 阅读数: 124
收藏 0 赞 0 分享

本文实例为大家分享了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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多