I am building an app that has a "master" button in the middle of the screen. Whenever the button is pressed(AND HELD), the app should be dimming the screen(to lowest level possible), and when the button is released, it should reset the screen to it's original state. Here's a piece of code of how my app handles the touch and release event of the button,

holdButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                lowerScreenBirghtness
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                resetScreenBirghtness();
                }
            }
            return true;
        }
    });

I have made some research and found that some had issues with phones that have automatic brightness control on. I am aware of a possible way to overwrite this setting through the app with the "WRITE_SETTINGS" permission. Any help would be appreciated. Please be as clear as possible as I am still learning this platform. Thanks

有帮助吗?

解决方案

If you want to control the screen brightness then you have to set it to manual mode because many devices has various problems when you are changing the brightness in an activity and the system setting is auto. So your methodology should be as follow:

  1. Read and store the current brightness level from the system settings.
  2. Do whatever you want in your app.
  3. Restore the saved level into the system settings.

It's up to you, as the developer, to respect the user's original setting of the brightness. So your thinking to manipulate the auto brightness setting is actually the best practice as long as you respect the user.

Here are two helpful functions, the first one reads the current level (0-255) and returns -1 if brightness is in auto mode or -2 if it cannot read the setting for some reason. The second one sets the brightness level to what you want and if you pass -1 as the brLevel it sets the brightness to auto.

    public static int getBrightnessLevel(Context cx) {
        int result = -1;

        if (cr == null) cr = cx.getContentResolver();

        if (cr != null) {
            boolean isAuto = false;

            try {
                isAuto = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS_MODE, 0) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 
            } catch (Exception ex) {
                isAuto = false;
            }

            if (isAuto) {
                result = -1;
            } else {
                try {
                    result = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
                } catch (Exception ex) {
                    result = -2;
                }               
            }
        } else {
            result = -2;
        }

        return result;
    }

    public static void setBrightnessLeve(Context cx, int brLevel) {
        if (cr == null) cr = cx.getContentResolver();

        if (cr != null) {
            try {
                if (brLevel >= 0) {
                    Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                    Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brLevel);
                } else {
                    Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
                }
            } catch (Exception ex) {
                //Do Nothing...
            }
        }
    }

I have developed a brightness widget which controls the screen brightness for the entire system and only for an app, here is the method that I am using:

  1. User selects his desired level (10 - 100) via a slide control. Be careful here not to let the user selects something smaller than 10% or the screen will go dark and he will be unable to restore the brightness because he will not see anything.

  2. I am changing the brightness setting in the system settings to reflect the user selection.

  3. I am firing up a non ui activity with an intent extra of the user selected brightness in order to set the screen brightness, because the setting change has not immediate effect.

For completeness here is the activity code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

public class act_brightnesshelper extends Activity {
    //----- Private Static Members -----
    private static final String     TAG = "act_brightnesshelper";
    //----- Private Static Members END -----



    //----- Private Members -----
    private int brightnessLevel;
    //----- Private Members END -----   



    //----- Activity Overrides -----
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        brightnessLevel = this.getIntent().getExtras().getInt("brightnessLevel");
    }

    @Override
    public void onAttachedToWindow() {
        Log.d(TAG, "onAttachedToWindow brightnessLevel = " + brightnessLevel);
        super.onAttachedToWindow();

        WindowManager.LayoutParams lp = getWindow().getAttributes();

        try {
            lp.screenBrightness = brightnessLevel / 255f;
            getWindow().setAttributes(lp);
        } catch(Exception ex) {
            //Do Nothing...
        }

        //Finish the Activity after 500ms...
        Thread WaitThread = new Thread(){
            @Override  
            public void run() {
                try {Thread.sleep(500);} catch (InterruptedException e) {}
                finish();
            }           
        };
        WaitThread.start();     
    }
    //----- Activity Overrides END----- 
}

Of course your app has different functionality but if you choose to go this way you can get what are you need from my code. Finally as you said, you will need the

android.permission.WRITE_SETTINGS

permission.

Hope this helps...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top