How to enable proximity sensor in android

☆樱花仙子☆ 提交于 2019-12-06 15:41:11

Check the changes submitted for this code, https://github.com/jitsi/jitsi-android/compare/6d356fa637ae...7aba481d773f

getPowerManager().newWakeLock( 32, "proximity_off");
astuter

As from my understanding wakelock is useful to keep screen on. I wasn't successful to turn screen on & off using it.

So, first I dimmed screen brightness as low as possible and then made all GUI elements unclickable. This way I solved my problem.

Following is method:

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub  
    WindowManager.LayoutParams params = this.getWindow().getAttributes();

    if (event.values[0] == 0) {
        //TODO Store original brightness value
        params.screenBrightness = 0.004f;
        this.getWindow().setAttributes(params);
        enableDisableViewGroup((ViewGroup)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),false);
        Log.e("onSensorChanged","NEAR");

    } else {
        //TODO Store original brightness value          
        params.screenBrightness = -1.0f;
        this.getWindow().setAttributes(params);                     
        enableDisableViewGroup((ViewGroup)findViewById(R.id.YOUR_MAIN_LAYOUT).getParent(),true);
        Log.e("onSensorChanged","FAR");  
    }       
}  

From here, I took reference to disable touch for entire screen's view.

Try this shortcut:

PowerManager manager = getPowerManager();
WakeLock lock = manager.newWakeLock( 32, "proximity_off");
lock.acquire();

The above code doesn't even need the proximity sensor changed events to be listened. 32 is for a hidden WakeLock field that is used by the InCallActivity of the Phone and other such similar applications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!