我有一个很棒的工作感应代码,在我将targetSdkVersion从11更改为19之后就被破坏了。我根本没有改变任何其他内容。
我想知道以前是否有人遇到过这个问题。或者这是一个已知的问题。
此致
答案 0 :(得分:0)
zgulser我不知道你对传感器做了多少工作,但这是接近传感器的介绍性代码,我试着希望这对你有帮助..
public class MainActivity extends Activity implements SensorEventListener
{
private SensorManager sensorManager;
TextView tVProximity;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tVProximity = (TextView)findViewById(R.id.tVProximity);
sensorManager= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor proximitySensor= sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximitySensor == null)
{
Toast.makeText(MainActivity.this,"No Proximity Sensor Found! ",Toast.LENGTH_LONG).show();
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
}
}
@Override
protected void onResume()
{
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause()
{
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
if(event.values[0]==0){
tVProximity.setText("You are Near: "+String.valueOf(event.values[0]));
try
{
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
// r.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
else{
tVProximity.setText("You are Far: "+String.valueOf(event.values[0]));
}
}}
}