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.

Tony Allevato
  • 6,369
  • 1
  • 27
  • 33
Mark Scheel
  • 2,923
  • 1
  • 18
  • 23

2 Answers2

44

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);
Tony Allevato
  • 6,369
  • 1
  • 27
  • 33
  • 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. – Mark Scheel Sep 04 '13 at 04:04
  • The Compass sample also uses the layout attribute; please see https://github.com/googleglass/apk-compass-sample/blob/master/res/layout/layout_compass.xml where `android:keepScreenOn` is set. – Tony Allevato Sep 04 '13 at 18:26
  • 4
    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? – Jakub K Jan 20 '14 at 13:27
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" />
Manuel M
  • 745
  • 1
  • 8
  • 23
  • 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() – Even Cheng Nov 01 '14 at 22:59