Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Turn off display without triggering sleep/lock screen - Turn on with Touchscreen

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.

like image 766
NebulaSleuth Avatar asked Mar 02 '12 18:03

NebulaSleuth


1 Answers

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.

like image 93
Bharath Lakshman Avatar answered Oct 07 '22 18:10

Bharath Lakshman