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