简体   繁体   English

如何在Android中启用接近传感器

[英]How to enable proximity sensor in android

I have configured proximeter successfully in my code. 我已经在代码中成功配置了近程仪。 Now I want to turn off & on screen programmatically. 现在,我想以编程方式关闭和打开屏幕。 Code of sensor is working just fine and following method is also getting called. 传感器的代码工作正常,还调用了以下方法。

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    WakeLock screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"screenWakeLock");

    if (event.values[0] == 0) {
        screenWakeLock.acquire();
        Log.e("onSensorChanged","NEAR");
    } else {
        if (screenWakeLock != null) {
               if(screenWakeLock.isHeld())
                  screenWakeLock.release();
               screenWakeLock = null;
        }
        Log.e("onSensorChanged","FAR");  
    }
} 

My problem is that screen is not getting turn off. 我的问题是屏幕无法关闭。 I also have added required permission in code. 我还在代码中添加了所需的权限。 What else could be the reason ??? 还有什么可能是原因?

检查为此代码提交的更改, https://github.com/jitsi/jitsi-android/compare/6d356fa637ae...7aba481d773f

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

As from my understanding wakelock is useful to keep screen on. 根据我的理解, wakelock对于保持屏幕打开很有用。 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. 因此,首先我将屏幕亮度调得尽可能低,然后使所有GUI元素不可点击。 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. 32是用于PhoneInCallActivity和其他类似应用程序使用的隐藏WakeLock字段。

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

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