简体   繁体   English

Android从小部件更改屏幕亮度

[英]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 更改屏幕亮度系统设置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.我有一个带有按钮的主要活动,当我单击按钮时,可以更改屏幕的亮度。

  1. I have a widget with buttons. 我有一个带有按钮的小部件。 I have confirmed that the RefreshScreen class is being called from a widget buttons. 我已经确认正在通过小部件按钮调用RefreshScreen类。 I call this class from AppWidgetProvider. 我从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 / BackupManagerService(137):dataChanged但没有参与者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 10-21 12:20:21.579:W / InputManagerService(137):窗口已经聚焦,忽略了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. 如果您在显示活动之前取消活动,则将无法刷新系统亮度...尝试将finish()放入活动onResume()中...这可能会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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