简体   繁体   English

如何控制Android接近传感器?

[英]How to control Android proximity sensor?

Can someone provide an example as to how to use the proximity sensor? 有人可以举例说明如何使用接近传感器吗? Please describe some event and how to use them. 请描述一些事件以及如何使用它们。

Every android mobile is shipped with sensors to measure various environmental conditions.Proximity sensor measures the distance that some object is from the device. 每个Android手机都附带传感器来测量各种环境条件。接近传感器测量某个物体距设备的距离。 It is often used to detect the presence of a person's face next to the device. 它通常用于检测设备旁边是否存在人脸。

Some proximity sensors only support a binary near or far measurement. 一些接近传感器仅支持二进制近或远测量。 In this case, the sensor should report its maximum range value in the far state and a lesser value in the near state. 在这种情况下,传感器应报告远态的最大范围值和近状态的较小值。

package com.exercise.AndroidProximitySensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidProximitySensorActivity extends Activity {
    /** Called when the activity is first created. */

 TextView ProximitySensor, ProximityMax, ProximityReading;

 SensorManager mySensorManager;
 Sensor myProximitySensor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ProximitySensor = (TextView)findViewById(R.id.proximitySensor);
        ProximityMax = (TextView)findViewById(R.id.proximityMax);
        ProximityReading = (TextView)findViewById(R.id.proximityReading);

        mySensorManager = (SensorManager)getSystemService(
          Context.SENSOR_SERVICE);
        myProximitySensor = mySensorManager.getDefaultSensor(
          Sensor.TYPE_PROXIMITY);

        if (myProximitySensor == null){
         ProximitySensor.setText("No Proximity Sensor!"); 
        }else{
         ProximitySensor.setText(myProximitySensor.getName());
         ProximityMax.setText("Maximum Range: "
           + String.valueOf(myProximitySensor.getMaximumRange()));
         mySensorManager.registerListener(proximitySensorEventListener,
           myProximitySensor,
           SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    SensorEventListener proximitySensorEventListener
    = new SensorEventListener(){

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onSensorChanged(SensorEvent event) {
   // TODO Auto-generated method stub

   if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
    ProximityReading.setText("Proximity Sensor Reading:"
      + String.valueOf(event.values[0]));
   }
  }
    };
}

The layout xml for the above code is given below: 上面代码的布局xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<TextView  
    android:id="@+id/proximitySensor"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:id="@+id/proximityMax"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
    android:id="@+id/proximityReading"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

In the above example we have 3 textview's. 在上面的例子中,我们有3个textview。 The first one is to display if the device supports proximity sensor, the second to display the maximum range supported by the sensor and the third one to display the current reading. 第一个是显示设备是否支持接近传感器,第二个显示传感器支持的最大范围,第三个显示当前读数。

Hope this has given you a basic idea about proximity sensors. 希望这能让您了解接近传感器的基本概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM