如何控制Android接近传感器? [英] How to control Android proximity sensor?

查看:145
本文介绍了如何控制Android接近传感器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供有关如何使用接近传感器的示例吗?请描述一些事件以及如何使用它们.

Can someone provide an example as to how to use the proximity sensor? Please describe some event and how to use them.

推荐答案

每个android mobile都随附有用于测量各种环境条件的传感器.接近传感器可测量某些物体与设备之间的距离.它通常用于检测设备旁边人脸的存在.

Every android mobile is shipped with sensors to measure various environmental conditions.Proximity sensor measures the distance that some object is from the device. 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]));
   }
  }
    };
}

以下代码给出了上述代码的布局xml:

The layout xml for the above code is given below:

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

在上面的示例中,我们有3个textview.第一个用于显示设备是否支持接近传感器,第二个用于显示传感器支持的最大范围,第三个用于显示当前读数.

In the above example we have 3 textview's. 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.

这篇关于如何控制Android接近传感器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆