简体   繁体   中英

Android changing screen brightness from a widget

I am trying to change screen brightness from a widget. Basically, I have followed the suggestion from the following page. Changing the Screen Brightness System Setting Android

So far, this is what I have been able to do. 1. I have a main activity with buttons, when I click on the button, I can change the brightness of the screen.

  1. I have a widget with buttons. I have confirmed that the RefreshScreen class is being called from a widget buttons. I call this class from AppWidgetProvider. Problem The screen brightness does not change. When I run the problem with debug mode, I get the following warnings.

    10-21 12:20:21.579: W/BackupManagerService(137): dataChanged but no participant pkg='com.android.providers.settings' uid=10086

    10-21 12:20:21.579: W/InputManagerService(137): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40f56aa8

I have the following permission set.

I have the following code to change the brightness. I'm sorry for confusing code but I have two buttons. When Brightness button is changed, the screen is supposed to get the brightest. When Volume button is changed, the screen is supposed to be half way between the darkest and the brightest.

Any input is greatly appreciated. Thank you.

public class RefreshScreen extends Activity {
    public static String BRIGHTNESS_VALUE = "BRIGHTNESS_VALUE";
    public static String BRIGHTNESS_CHANGE = "BRIGHTNESS_ACTION";
    public static String VOLUME_CHANGE = "VOLUME_ACTION";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String value = intent.getStringExtra(BRIGHTNESS_VALUE);

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

        if (value.equals(BRIGHTNESS_CHANGE)) {
            Toast.makeText(this, "BRIGHTNESS CHANGE Clicked", Toast.LENGTH_LONG).show();
            android.provider.Settings.System.putInt(getContentResolver(),
                    android.provider.Settings.System.SCREEN_BRIGHTNESS, 255);

            layoutParams.screenBrightness = 1F;
            getWindow().setAttributes(layoutParams);
        }
        if (value.equals(VOLUME_CHANGE)) {
            Toast.makeText(this, "VOLUME CHANGE Clicked", Toast.LENGTH_LONG).show();
            android.provider.Settings.System.putInt(getContentResolver(),
                    android.provider.Settings.System.SCREEN_BRIGHTNESS, 125);

            layoutParams.screenBrightness = 0.5F;
            getWindow().setAttributes(layoutParams);
        }
        finish();
    }

}

It seems that your activity is the one who will update the config from the window... (by that i mean, your activity is the dummy activity from the other question). If you kill your activity before it is shown, you won't be able to refresh the system brightness... Try to put the finish() in the activity onResume()... this might work correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM