7

I have a GDK immersion application, where the launcher Activity acquires aSCREEN_DIM_WAKE_LOCK WakeLock. The app also has a Service which will receive chat messages and starts an Intent for an Activity to display each one. Whenever the message Activity is opened, I want to brighten the screen. However, all of the methods I have found do not seem to work.

For example, adding the following into onResume has no effect:

    Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = 1.0f;
    getWindow().setAttributes(lp);

To better illustrate the problem, here is the sequence of events in my app:

  1. Activity A starts and acquires a SCREEN_DIM_WAKE_LOCK. Activity A dims after a short time.
  2. Service B receives a chat message over the network and creates an Intent for Activity C
  3. Activity C opens, sets the screen brightness as shown above, but remains dimmed

How can I get the screen to brighten?

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • 1
    The code you provided is working great for me in an activity in XE12. I was able to dim the screen, wait a second, and then reverse the dimming returning it to the previous brightness. Are you able to dim it? – mimming Feb 05 '14 at 16:59
  • @JennyMurphy In my situation, I start with Activity A, wait for it to dim, move to Activity B. I find that Activity B won't brighten using just this code. I did get it to work by acquiring a wakelock with the ACQUIRE_CAUSES_WAKEUP flag though. I put that code in my answer below. – Eric Levine Feb 05 '14 at 17:26

2 Answers2

5

I was able to find a solution by acquiring a SCREEN_BRIGHT_WAKE_LOCK with the ACQUIRE_CAUSES_WAKEUP flag in onResume. For example:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
//..screen will stay on during this section..
wl.release();
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
1

Try to put the code before the call to setContentView in onCreate (the brightness level won't update otherwise), something like:

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);

  WindowManager.LayoutParams lp = getWindow().getAttributes();
  lp.screenBrightness = 1.0f;
  getWindow().setAttributes(lp);
  // example
  setContentView(R.layout.main);
Magnus
  • 1,483
  • 11
  • 14
  • That didn't work. I also tried using Settings.System.putInt to change SCREEN_BRIGHTNESS to 255, but didn't have any luck. – Eric Levine Feb 04 '14 at 15:11