简体   繁体   English

如何让android在呼叫中做出反应并触发接近传感器?

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

I have an application which makes VoIp call. 我有一个应用程序可以调用VoIp。 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手机应用程序的样本提供了关于接近传感器的一些很好的信息。

android: turn off screen when close to face android:靠近脸部时关闭屏幕

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 以下是可以帮助您的代码..这演示了我们如何使用SensorManager Class访问接近传感器,这里接近传感器会触发它调用一个方法来更改图像视图,您可以尝试使用该方法的代码.... ,在这里获取整个源代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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