Android的 - 关闭显示屏,而不会触发睡眠/锁屏 - 打开触摸屏

问题描述:

我一直试图找到一种方法来关闭显示,从触摸触摸屏用户醒来。
该装置是在嵌入式环境中,其中该装置是片剂而所述用户不具有访问任何东西,除了触摸屏(无按钮在所有)。

I have been trying to find a way to turn off the display, and wake up from the user touching the touch screen.
The device is in an embedded environment where the device is a tablet and the user does not have access to anything except the touch screen (no buttons at all).

它连接到电源使电池不会是一个问题,但是当我发现没有任何活动我要关掉屏幕,它不是整天盯着他们的脸,不降低生活LCD背光。

It is connected to power so the battery won't be a problem, but when I detect no activity I want to turn off the screen so it isn't staring them in the face all day and doesn't reduce the life the LCD backlight.

我保持wakelock永久,并决定什么时候睡觉喽。

I maintain a wakelock permanently and decide when to sleep myself.

现在的问题是,当我关掉屏幕使用:

The problem is that when I turn off the screen using :

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);

该活动被暂停和停止。而单位不给触摸响应唤醒它。您需要preSS的电源按钮。在这一点上滑动解锁出现了。

The activity gets paused and stopped. And the unit does not respond to a touch to wake it up. You need to press the power button. At that point the "slide to unlock" shows up.

我要关闭显示器,然后保持运行,所以我可以检测到触摸屏事件,并重新打开显示屏上。

I want to turn off the display, and then stay running so I can detect a touch screen event and turn the display back on.

我也试过打开显示器亮度为0.1,其工作在某些设备上,但我需要它来工作的设备,只有变暗的显示。

I also tried turning the display to a brightness of 0.1, which works on some devices, but the device I need it to work on, only "dims" the display.

我也试过这样的:

// First Remove my FULL wakelock

//then aquire a partial wake lock (which should turn off the display)
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");

wl.acquire();

但这种方法无法关闭显示器。

however this method does not turn off the display.

终于想通了。希望它能帮助。 :)

Finally figured it out. Hope it helps. :)

  • 获取窗口管理器的一个实例。

  • Get an instance of WindowManager.

窗口管理器窗口管理=(窗口管理器)的Class.forName(android.view.WindowManagerImpl)。GetMethod的(getDefault,新的等级[0])。调用(NULL,新对象[0] );

创建一个全屏幕布局的XML(布局参数设置为 FILL_PARENT

Create a full screen layout xml(layout parameters set to fill_parent)

设置你的观点不点击,不可作为焦点,没多久点击等使触摸传递到您的应用程序和应用程序可以检测到它。

Set your view as not clickable, not focusable, not long clickable, etc so that touch is passed through to your app and the app can detect it.

view.setFocusable(假);
 view.setClickable(假);
 view.setKeepScreenOn(假);
 view.setLongClickable(假);
 view.setFocusableInTouchMode(假);

view.setFocusable(false);
view.setClickable(false);
view.setKeepScreenOn(false);
view.setLongClickable(false);
view.setFocusableInTouchMode(false);

创建类型的布局参数 android.view.WindowManager.LayoutParams 的LayoutParams的LayoutParams =新的LayoutParams();

Create a layout parameter of type android.view.WindowManager.LayoutParams. LayoutParams layoutParams = new LayoutParams();

像的高度设置布局参数,宽度等

Set layout parameter like height, width etc

layoutParams.height = LayoutParams.FILL_PARENT; 
layoutParams.width = LayoutParams.FILL_PARENT;
layoutParams.flags = 280; // You can try LayoutParams.FLAG_FULLSCREEN too
layoutParams.format = PixelFormat.TRANSLUCENT; // You can try different formats
layoutParams.windowAnimations = android.R.style.Animation_Toast; // You can use only animations that the system to can access
layoutParams.type = LayoutParams.TYPE_SYSTEM_OVERLAY;
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.verticalWeight = 1.0F;
layoutParams.horizontalWeight = 1.0F;
layoutParams.verticalMargin = 0.0F;
layoutParams.horizontalMargin = 0.0F;

  • 关键一步:可以设置多少百分比的亮度需要。 layoutParams.setBackgroundDrawable(getBackgroundDrawable(一));

  • Key step: You can set what percentage of brightness you need. layoutParams.setBackgroundDrawable(getBackgroundDrawable(i));

    private Drawable getBackgroundDrawable(int i) {
    int j = 255 - (int) Math.round(255D * Math.exp(4D * ((double) i / 100D) - 4D));
    return new ColorDrawable(Color.argb(j, 0, 0, 0));}
    

  • 最后补充以窗口管理器前面创建。

  • Finally add view to windowManager that you created earlier.

    windowManager.addView(视图的LayoutParams);

    请注意:您需要 SYSTEM_ALERT_WINDOW 许可奠定在屏幕上覆盖

    Note: You need SYSTEM_ALERT_WINDOW permission to lay an overlay on the screen.

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    

    曾经测试过这一点和它的作品。让我知道你是否会被卡住。

    Have tested this and it works. Let me know if you get stuck.