30

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).

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.

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

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.

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.

I also tried this:

// 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.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
NebulaSleuth
  • 833
  • 1
  • 7
  • 18
  • 1
    If I understand correctly, you succeeded with keeping your activity running and the remaining part of the question is how to switch off the screen. For this I suggest to implement an on-top auxiliary activity with no layout - in this case Android shows black screen. You can use android:theme="@android:style/Theme.NoTitleBar.Fullscreen" for full screen blackout. – Stan Jun 08 '12 at 10:22
  • 3
    For my case, the black screen done with a wake lock is not sufficient, because the screen backlight is still on, so even with a black fullscreen activity, it's still too bright in an entirely dark room - I can still see the device screen, but I'm not supposed to. – Mathias Conradt Jun 09 '12 at 13:12
  • reached something about this problem ?!! – Muhammed Refaat Aug 03 '16 at 09:14
  • Unfortunately not much. My solution was to wake up from other triggers instead. In my case that was to check if the device was being charged or not by registering a BroadcastReceiver. This was acceptable in my very specific situation. Another thing I played with and had some success with was using the sensors to detect device movement. Registering for a receiver of these events allows me to trigger a wake up by applying a wakelock when they occurred. Hope that helps. – NebulaSleuth Aug 06 '16 at 19:06

4 Answers4

7

Finally figured it out. Hope it helps. :)

  • Get an instance of WindowManager.

    WindowManager windowManager = (WindowManager) Class.forName("android.view.WindowManagerImpl").getMethod("getDefault", new Class[0]).invoke(null, new Object[0]);

  • 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(false);
    view.setClickable(false);
    view.setKeepScreenOn(false);
    view.setLongClickable(false);
    view.setFocusableInTouchMode(false);

  • 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;
    
  • Key step: You can set what percentage of brightness you need. view.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(view, layoutParams);

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.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
1

If you have services use wake lock but if you are doing it from Activity side its better idea to use flags to acquire flag.

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // For keeping screen on
        // You can blur screen or dim screen using following flags
        // FLAG_BLUR_BEHIND -   Blur screen
        // FLAG_DIM_BEHIND - Dim Behind

You can clear these flags like and impose other flags to dim screen or blur it. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Hope this will solve your problem. Optionally Explore other flags from http://developer.android.com/reference/android/view/Window.html

There is one more Fyi.. Calling hidden API in android to turn screen off

Community
  • 1
  • 1
kodeshpa
  • 549
  • 5
  • 14
  • If I remove the wakelock and use this method to prevent the unit from going to sleep, I still can't turn off the display without the unit stopping my activity. – NebulaSleuth Mar 02 '12 at 19:22
  • 1
    The good news: calling the hidden API function setScreenState turns off the display without pausing or stopping my activity. The bad news: It seems to disable the touch screen as well... – NebulaSleuth Mar 02 '12 at 22:50
  • @NebulaSleuth Did you find a solution yet? I also need the screen to still listen to touch events, while the screen is entirely dimmed. Thanks! – Mathias Conradt Jun 02 '12 at 13:57
  • 1
    Just throwing an idea here... Since you say you managed to turn the screen off without actually putting the phone to sleep, why not use the accelerometer to detect a tap on the screen and turn it back on? – Mircea Nistor Jun 05 '12 at 17:46
  • @MirceaNistor Afaik the accelerometer doesn't work when the screen is off (& phone at sleep). I have NOT managed to turn off the screen without putting the phone to sleep. NebulaSleuth mentioned he managed to use the hidden API, however, it doesn't work for me (http://pastebin.com/WQKR857z) - on Nexus One with Android 2.3.6. – Mathias Conradt Jun 09 '12 at 17:59
  • @ Mathias - I have never been able to solve this entirely. Basically I am in still just showing a black screen and putting the backlight to the blazingly bright minimum setting. – NebulaSleuth Aug 04 '12 at 01:36
0

I don't know if that helps you but you could simply use a wake lock so your scren keeps on and display an imageview which displays a black image

Alexander Fuchs
  • 1,358
  • 3
  • 19
  • 36
  • 8
    Thanks but this is actually the current status quo which we already have, which is unfortunately not sufficient, because the screen backlight is still on, so even with a black fullscreen activity, it's still too bright in an entirely dark room - I can still see the device screen, but I'm not supposed to. In my case, I need this for tablets installed in hotel rooms (as control panel for various stuff), where - when the room is entirely dark - there's not supposed to be any light at all, not even dimmed screen backlight. Only if the user touches the devices, the screen should turn on. – Mathias Conradt Jun 09 '12 at 13:12
  • 1
    I think the only way is to build a custom rom and modify the kernel. It's for the Samsung Galaxy Tab 1. But still need to check which kernel classes are the relevant ones. – Mathias Conradt Jun 09 '12 at 13:15
  • @MathiasLin Did you got which kernel files are the relevant ones to be edited for building this custom rom ? – Vins Oct 10 '13 at 10:01
  • @vins Don't remember, too long ago. – Mathias Conradt Oct 10 '13 at 13:19
  • Buy a tablet with an OLED display, then there will be no differnece if the screen would be turned off, or if you display just black backgorund for the whole screen, while the screen still will be active for touch input. – Krzysztof Cichocki Sep 01 '15 at 13:59
  • @MathiasConradt so, do you reached a solution to this problem ?!! I kinda stuck in it also – Muhammed Refaat Aug 03 '16 at 09:13
  • 1
    @MuhammedRefaat Can't remember, the project I worked on is already 4 years ago. At least, I didn't manage it via app code. Eventually one of the devs did it via kernel adjustments, but I cannot remember anymore. – Mathias Conradt Aug 06 '16 at 09:28
0

You can do one thing that is Acquire Partial Wakelock and turn the Screen Off using :

PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);
Sanober Malik
  • 2,765
  • 23
  • 30