GDK / APK for Google Glass - Keep screen from dimming
Asked Answered
S

2

24

What part of the code in the four sample APK projects listed here for Google Glass prevents the screen from dimming?

When I write my own APK and sideload it, after ten seconds with no tapping, the screen dims but does not turn off.

What manifest change or code change can I use to prevent the screen from dimming.

Thanks! Should there be a Google-Glass-GDK tag? If so add it please.

Seychelles answered 29/8, 2013 at 4:10 Comment(0)
B
45

There are a couple easy ways you can do this without requesting a wake lock:

  • Add the android:keepScreenOn="true" attribute to the root element of your layout.

  • Or, do the following in your onCreate method:

  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Baber answered 29/8, 2013 at 23:41 Comment(3)
That works, upvoted. What in the sample apps causes similar behavior? Stopwatch has the first, layout trick, so that's obvious, but Compass has neither of the tricks, is it the sensor reporting continually resetting the ten second clock? Thanks for clarifying.Seychelles
The Compass sample also uses the layout attribute; please see github.com/googleglass/apk-compass-sample/blob/master/res/… where android:keepScreenOn is set.Baber
I can't get either of these solutions to work, the screen keeps dimming. The link to the sample project references a file which no longer exists and I can't find mentioned setting anywhere in the new code. Is there a way to keep the screen on or was it abandoned for some reason?Simper
A
2

The only way that worked for me was by acquiring a wakeLock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK , TAG);
wakeLock.acquire(WAKE_LOCK_DURATION_IN_MILLIS);

You also need a permission for that:

<uses-permission android:name="android.permission.WAKE_LOCK" />
Ashleeashleigh answered 23/7, 2014 at 7:27 Comment(1)
This works for me since my Glass app starts with a service and use SurfaceHolder to hold the TimeCard. Can't find Activity to call getWindow()Pulver

© 2022 - 2024 — McMap. All rights reserved.