Alter screen brightness programmatically

HI,
I am trying to find a simple method to alter the screen brightness in an application that will only be viewed for a short period then after that is not required. I have found some example code created using android studio but i am having a few issues making these work under Android mode in processing. These examples also do a lot more than i actually need. I just need to dim the screen to its minimum value after a variable period of time.

Any help is much appreciated.

Cheers.

The following source code will allow the user to change the brightness of an Android device. The brightness range is from 1 to 255, with 1 being very dim and 255 being maximum brightness. To start:

  • List item

1.) ‘WRITE_SETTINGS’ must be checked in the menu Android/SketchPermissions. Select ‘OK’.
2.) Run source code on device. Should see a permissions screen initially.
3.) On right hand side of screen toggle ‘Allow permission’ to ON.
4.) Quit app and re-Run. Brightness change takes effect on second Run.
5.) Select ‘Display’ on the left hand side of the screen; should see the brightness slider appear on the right hand side of the screen. The position of the slider should match the value that you requested.

/*
 Menu Android/SketchPermissions should be set to 'WRITE_SETTINGS'
 */
 
import android.view.WindowManager;
import android.os.Build;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;

void dimScreen(int brightness) {

  try {
    //sets manual mode and brightness
    android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)
    //this will set the brightness (1->255)
    android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightness);
    //refreshes the screen
    int br = android.provider.Settings.System.getInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = (float) br / 255;
    getWindow().setAttributes(lp);
  }
  catch (Exception e) {
  }
}

void setup() {
  fullScreen();
  // User has to give permission first
  Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getContext().getPackageName()));
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);
  // min -> dark = 1 : max -> bright = 255
  dimScreen(1);
}

void draw() {

}

Device screenshot_1:

Device screenshot_2: Brightness = 255

Hi,
And thanks for the reply. and also thanks for the code example - i will give it a go and see if i can amalgamate it into my main program.

I actually did find another example (see below) however my main program uses the ketai library (camera) and the example code as given seems to interfere with the operation of the camera callback.

Again thanks for your help with this.

Cheers.

import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.text.Editable;
import  android.graphics.Color;
import android.widget.Toast;
import android.os.Looper;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;


import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

Activity act;
Context mC;

@Override 
  public void onStart() {
  super.onStart();

  act = this.getActivity();
  mC= act.getApplicationContext();

  setBrightness(0.2);
}

void setBrightness(float val) {
  WindowManager.LayoutParams lp = act.getWindow().getAttributes(); 
  lp.screenBrightness = val; 
  act.getWindow().setAttributes(lp);
}

void setup() { 
  fullScreen();
  orientation(PORTRAIT);
  fill(0);
  textAlign(CENTER,CENTER);
  textSize(40);

  Looper.prepare();
} 

void draw() { 
  background(255);
  text("Current brightness="+map(mouseX, 0, width, 0, 1), width/2, height/2);
}

void mousePressed() {
  act.runOnUiThread(new Runnable() {
    public void run() {
      setBrightness(map(mouseX, 0, width, 0, 1));
    }
  }
  );
}

That’s a nice little demo that allows the user to set screen brightness by touching the screen (dim on left, bright on right). I’m surprised that you’re able to do that without getting permission, but it seems to work just fine without it. Depending on what you’re trying to do, your example may suit your needs better than what I posted. You could likely also use a slider to control brightness if desired.