使用Android的接近传感器 [英] Using proximity sensor in android
问题描述
我想用接近传感器在我的项目。我搜索了距离感应器教程。而且我发现了一个传感器,在教程的http://www.vogella.com/articles/AndroidSensor/article.html#sensoroverview_manager.我试图用接近传感器如下code:
I want to use proximity sensor in my project. I searched for proximity sensor tutorial. And I found a sensor tutorial at http://www.vogella.com/articles/AndroidSensor/article.html#sensoroverview_manager. I tried to use proximity sensor as following code:
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)
{
Toast.makeText(getApplicationContext(), "working", Toast.LENGTH_SHORT).show();
}
}
我的活动
正在实施 SensorEventListener
。但它不工作。我是否需要使用使用接近传感器的任何许可?还是我的code是错误的,这样做的东西。您的帮助将是非常美联社preciated。谢谢
My Activity
is implementing SensorEventListener
. But its not working. Do I need to use any permission to use proximity sensor? Or My code is wrong for doing this stuff. Your help will be very appreciated. Thank you
推荐答案
你是注册你的听众。在您的活动:
you are to register your listener. In your activity:
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
然后在你被覆盖的onSensorChanged()方法,你可以做你的东西带传感器的数据。
and then in you overriden onSensorChanged() method you can do your stuff with sensor data.
很抱歉,如果我的回答是为时已晚=)
Sorry if my answer is too late =)
这篇关于使用Android的接近传感器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!