我写了一个简单的活动来监听接近传感器(http://thecodeartist.blogspot.co.il/2011/01/proximity-sensor-on-android-gingerbread.html),我正试图在我的模拟器上测试它。该应用适用于真实设备(在Nexus 4上查看)。
我一直在读某个地方,我应该修改DDMS中“位置控制”中的一些值以调用该警报(例如,https://stackoverflow.com/questions/15048559/can-i-test-proximity-alert-in-the-emulator),但我仍然没有得到它 - 如何位置与接近传感器有关,只能提醒“远”或“近”?
我真的很感谢你的帮助, 感谢。
这是我的代码(通用和简单):
package com.example.proximitysensor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximitySensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
protected void onResume()
{
super.onResume();
mSensorManager.registerListener(this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 0)
{
Toast.makeText(this, "near", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "far", Toast.LENGTH_LONG).show();
}
}
}