Change brightness of display on Android

Hello,

I am developing an app for Android, and I would like to integrate there ability to change displey brightness of phone. I mean system brightness, not brightness of my app. How can I do that? I would be very grateful for every suggestion.

Thank you.

There isn’t a simple answer to achieve what you want I’m afraid. I know because I’ve done it!
Android requires that you first get permission from the user before it allows them to change system settings. So you’ll need to get that. In theory, Unity lets you ask for permissions using

Permission.RequestUserPermission(string permission)

In this case permission = “android.permission.WRITE_SETTINGS”
However, I couldn’t get this to work and ended up writing an android plugin to get the permission!

This is just the backend stuff - you’ll also need to create a nice UI dialog to persuade the user into giving you permission, along with the reason why you need it (per android dev guidelines).

Next, once you have asked and gained the permission you need to actually change the brightness and to do this you’ll need to write (another) android plugin.

First you check to see if the screen brightness mode is automatic or manual. If it’s automatic you need to change it to manual.

Finally, you can actually set the screen brightness to your value. Here’s the java code:

int brightnessMode = android.provider.Settings.System.getInt(getContentResolver(), 
        android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
{
        android.provider.Settings.System.putInt(getContentResolver(), 
                android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, 
                android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
 }

 Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, <yourDesiredValue>);

On iOS you don’t need to get permission to change the device brightness so it’s quite a bit simpler - which is nice! And Android lets you change the brightness of the screen for just your app (rather than the whole device) without needing any permissions first. This is the route I eventually ended up going down for my unity asset Dimmer - Dimmer | Integration | Unity Asset Store

Anyway, hope this is helpful.