Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...