简体   繁体   中英

Android Proximity Sensor Not Working

I had a great working proximity code which is broken just after I've changed the targetSdkVersion from 11 to 19. I didn't change anything else at all.

I wonder if there is anybody faced this issue before. Or is this sort of a known issue.

Regards

zgulser I don't know how much work did you do on the sensor but this is the introductory code for the proximity sensor that I tried hope this helps you..

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]));

}

}}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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