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.
Any solutions for your problem? I am having a similar issue.
Answer by Riskjockey · Mar 27, 2019 at 09:37 AM
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 - https://assetstore.unity.com/packages/tools/integration/dimmer-141555
Anyway, hope this is helpful.
@Riskjockey hey, Under Which Tag in android manifest file should I paste the above code? and how do I change the "yourDesiredValue" by using a slider?
You mean the "android.permission.WRITE_SETTINGS"? It should be fine anywhere under the manifest tag. eg
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp">
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>
As for attaching a slider to the value... you'll need a bunch of code, and I'm guessing at least a week of program$$anonymous$$g time. Or you could just check out Dimmer on the Unity app store and it will do all of that for you.