1

I was inspired by the Costco app to try to do something to my App. For Costco, when you pull up the barcode, it maxes out screen brightness to make it easier to scan. There is nothing in the permissions for that app, so it is somehow being done programatically. I have a QR code on my app, when someone pulls up I would like to max screen brightness just on that screen for scanning '''

   public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        ProgressDialogHelper.showProgress(MainActivity.this);   
        String host = Uri.parse(request.getUrl().toString()).getHost();
        urlData = request.getUrl().toString();

        String upBrightness = "Link of BCODE PAGE";
        if (urlData.equals(upBrightness)) {
            WindowManager.LayoutParams MainActivity = getWindow().getAttributes();
            MainActivity.screenBrightness = 1F;
            getWindow().setAttributes(MainActivity);
        }

''' I am not having any luck getting this to work, any ideas?

2 Answers2

2

So after wasting hours, I have figured out the code works just fine. Brightness means nothing to the Android emulators and you need to test it on a real phone. I feel stupid, but just to let everyone know the code works.

Edit: You also do not need to add the permission. "Write settings" seems like a scary permission for the end user to accept and I wanted to avoid it.

1
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f;// max value - 100 / 100.0f i.e 1.0f;
getWindow().setAttributes(lp);

The value for brightness via Layout param is defined here https://developer.android.com/reference/android/view/WindowManager.LayoutParams#BRIGHTNESS_OVERRIDE_FULL

The max value that you can provide for screen brightness setting is 0 - 255 also adjusting the brightness of the device requires to write permission. check the link here - https://developer.android.com/reference/android/provider/Settings.System#SCREEN_BRIGHTNESS

you would also need to add following permission in manifest.

<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
  • This is just the question's code with a different variable name, the wrong brightness value, a comment that doesn't make sense (`100 / 100.0f` is `1.0f`, not `0.2f`), and an irrelevant permission (they're not trying to write settings, that permission is not required) – Ryan M Jun 24 '20 at 00:55
  • 1
    check this answer here for util class for setting brightness programmatically - https://stackoverflow.com/a/61636289/1994089 – Shekhar Suman Jun 24 '20 at 22:33
  • For versions API 23 and up the user will need to opt in for the write setting permission: https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS – Kenny Sexton Mar 17 '22 at 20:44