I have an application which makes VoIp call. During those call, I would like the screen to react exactly as it does during a normal call. That is, i want the screen to disable all events and turn off when the user triggers the proximity sensor. Of course, when the user moves away his ear from the phone, I want the phone to turn the screen on and enable back all events. I tried before to turn off and on the screen myself. Never worked. So I thought i'd used this solution.
Asked
Active
Viewed 3,001 times
2 Answers
1
There is a similar question I found which gives some great info on the Proximity Sensor using samples from the Android Phone app.

Community
- 1
- 1

mattdonders
- 1,328
- 1
- 19
- 42
-
Thanks for the link. I read it and used this code to turn the sensor on: PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mProximityWakeLock = pm.newWakeLock(32,"Salut"); // 32 correspond to constant PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK // automatically when the sensor detects a close object. if (!mProximityWakeLock.isHeld()) { mProximityWakeLock.acquire(); } and a release to turn the sensor off. – Jean-Simon Vaillant Jul 17 '12 at 18:37
1
Here is the code which may help you ..this demonstrate how we can access proximity sensor using SensorManager Class,here the proximity sensor will triggered an it calls a method which changes an image view you can try your code on that method...., get the Whole Source code here
public class MainActivity extends Activity {
SensorManager sm;
Sensor ProximitySensor;
boolean state=true;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txstate= (TextView) findViewById(R.id.txstate);
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
ProximitySensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sm.registerListener(proximitySensorEventListener, ProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
}
SensorEventListener proximitySensorEventListener = new SensorEventListener()
{
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
{if((int)(event.values[0])==1)
{
call();
}
}
}
private void call()
{
ImageView im = (ImageView) findViewById(R.id.imageView1);
if(state)
{
state =false;
im.setImageResource(R.drawable.smile);
}
else
{
state=true;
im.setImageResource(R.drawable.sad);
}
}
};
}

sarath kumar
- 134
- 4