简体   繁体   English

如何获取Android接近传感器的当前值?

[英]How to get current value of Android's proximity sensor?

Is it possible to get the current value of the Proximity Sensor in Android? 是否可以获取Android中接近传感器的当前值?

I know that I can use SensorManager and Sensor and register a state changed listener, but I have no need to be notified of every state change, so it would be highly inefficient since this code is being run in a service. 我知道我可以使用SensorManager和Sensor并注册一个状态已更改的侦听器,但是无需通知每个状态更改,因此由于此代码正在服务中运行,因此效率极低。 Also, my code won't know the value until a state change has occurred (what if the value hasn't changed...how do I know what it is? Instead, rather than registering a listener, I just want to say: 另外,在状态发生变化之前,我的代码将不知道该值(如果该值未发生变化,那怎么知道...我怎么知道它是什么呢?而不是注册一个侦听器,我只想说:

proximitySensor.getCurrentDistance();

Is this possible? 这可能吗?

Thanks 谢谢

After looking through the documentation, it looks like you can get the distance in centimeters by subscribing to the SensorEvent and looking at the data being passed back. 阅读完文档后,您可以通过订阅SensorEvent并查看传递回的数据来获得以厘米为单位的距离。

There is a good example of starting to use the Proximity sensor here: Android Proximity Sensor Example 这里有一个很好的示例,可以开始使用接近传感器: Android接近传感器示例

After reading a bit further into the Android docs , it looks like the array values[0] returns a value in centimeters. 在深入阅读Android 文档之后 ,数组values[0]返回的值以厘米为单位。 Note, look at the docs, some sensors only return binary values, meaning that the device is either near or far. 请注意,请查看文档,某些传感器仅返回二进制值,这意味着设备接近或远离。

To get access the device sensor using SensorManager, you have to call getSystemService(SENSOR_SERVICE) . 要使用SensorManager访问设备传感器,必须调用getSystemService(SENSOR_SERVICE)。 Here is the example: 这是示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".SensorActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/far" />

</RelativeLayout>

Here is the java class: 这是java类:

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ImageView;

public class SensorActivity extends Activity implements SensorEventListener {
 private SensorManager mSensorManager;
 private Sensor mSensor;
 ImageView iv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.sensor_screen);
  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
  iv = (ImageView) findViewById(R.id.imageView1);
 }

 protected void onResume() {
  super.onResume();
  mSensorManager.registerListener(this, mSensor,
    SensorManager.SENSOR_DELAY_NORMAL);
 }

 protected void onPause() {
  super.onPause();
  mSensorManager.unregisterListener(this);
 }

 public void onAccuracyChanged(Sensor sensor, int accuracy) {
 }

 public void onSensorChanged(SensorEvent event) {
  if (event.values[0] == 0) {
   iv.setImageResource(R.drawable.near);
  } else {
   iv.setImageResource(R.drawable.far);
  }
 }
}

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

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