title: Proximity Sensor- Silence call application
body: <p>I am trying to build an android application that silences a call when a person waves his/her hand over the phone as it starts ringing/vibrating using the Proximity sensor. </p>
  
  <p>I am new to android development.
  I have tried to make the application check if the phone is in State-Rining. If it is, then the app checks if the value  of the proximity sensor ==0 (near value). If the value for proximity sensor is 0, it implies that the sensor was cover during the ringing ad hence it should mute the call.
  But it is still in ringing state. </p>
  
  <p>I think the problem is that the ServiceReceiver only checks for the value of the proximity sensor just as the  phone starts to ring. If the sensor value is not zero at that instant, the phone doesn't get silenced. 
  So how can I make it check for a change in value of proximity sensor throughout the ringing or incoming call such that as soon as the sensor value turns to zero, call is silenced</p>
  
  <p>The first class "mainactivity" contains the code for proximity sensor.</p>
  
  <pre><code>package example.ringcheck;
  
  
  import android.app.Activity;
  import android.content.Context;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.media.AudioManager;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.widget.TextView;
  
  public class MainActivity extends Activity implements SensorEventListener {
  static TextView proxText;
  SensorManager sm;
  Sensor proxSensor;
  static float proxValue = 8;
  // static boolean muteCall = false;
  private AudioManager amanager;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      sm = (SensorManager) getSystemService(SENSOR_SERVICE);
      proxSensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
      proxText = (TextView) findViewById(R.id.ProximityTextView);
  
      sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
  
  }
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  @Override
  public void onAccuracyChanged(Sensor arg0, int arg1) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onSensorChanged(SensorEvent event) {
  
  
      // TODO Auto-generated method stub
      proxText.setText(String.valueOf(event.values[0]));
      proxValue = event.values[0];
      Log.d("SENSOR VALUE", "********" + proxValue + "*******");
  
  
  
      }
  
  }
  </code></pre>
  
  <p>The second class "ServiceReceiver" contains the code for proximity sensor.</p>
  
  <pre><code>package example.ringcheck;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.media.AudioManager;
  import android.telephony.TelephonyManager
  import android.util.Log;
  
  public class ServiceReceiver extends BroadcastReceiver {
  
  private AudioManager amanager;
  // static boolean isRinging = false;
  
  public void onReceive(Context context, Intent intent) {
  
      if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
              TelephonyManager.EXTRA_STATE_RINGING)) {
          // isRinging = true;
              if (MainActivity.proxValue == 0) {
                  amanager = (AudioManager) context
                          .getSystemService(Context.AUDIO_SERVICE);
                  amanager.setRingerMode(0x00000000);
                  // isRinging = false;
  
              }
  
      Log.d("MPR", "Its Ringing");
  
  
  
      }
  }
  
  
  }
  </code></pre>
  
  <p>Thanks in advance :-)</p>