简体   繁体   中英

How can I make android react as in a call and the proximity sensor is triggered?

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.

There is a similar question I found which gives some great info on the Proximity Sensor using samples from the Android Phone app.

android: turn off screen when close to face

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

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