更改屏幕亮度系统设置 Android

发布于 2024-11-20 00:33:30 字数 277 浏览 4 评论 0原文

我正在尝试通过服务更改屏幕亮度,如下所示:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

问题是这不起作用。好吧,实际上它成功地更改了亮度设置,但屏幕亮度实际上并没有改变,直到我进入手机设置,查看新值并点击“确定”。

设置值后我必须做些什么才能改变亮度吗?

I'm attempting to change the screen brightness from withing a service, like so:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

Problem is that is doesn't work. Well, actually it succeeds in changing the brightness setting, but the screen brightness doesn't actually change till I go into the phones settings, look at the new value and hit Ok.

Is there something I have to do after setting the value to get the brightness to change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

擦肩而过的背影 2024-11-27 00:33:30

我也遇到过在服务中更改屏幕亮度的同样问题,几天前我成功解决了这个问题(并更新了我的应用程序 具有亮度功能的电话时间表;))。
好的,这是您放入服务中的代码:

// This is important. In the next line 'brightness' 
// should be a float number between 0.0 and 1.0
int brightnessInt = (int)(brightness*255);

//Check that the brightness is not 0, which would effectively 
//switch off the screen, and we don't want that:
if(brightnessInt<1) {brightnessInt=1;}

// Set systemwide brightness setting. 
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightnessInt);

// Apply brightness by creating a dummy activity
Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("brightness value", brightness); 
getApplication().startActivity(intent);

请注意,在上面的代码片段中,我使用了两个亮度变量。一个是 brightness,它是 0.0 到 1.0 之间的浮点数,另一个是 brightnessInt,它是 0 到 255 之间的整数。这样做的原因是 < code>Settings.System 需要一个整数来存储系统范围的亮度值,而您将在下一个代码片段中看到的 lp.screenBrightness 需要一个浮点数。不要问我为什么不使用相同的值,这就是 Android SDK 中的方式,所以我们只能接受它。

这是 DummyBrightnessActivity 的代码:

public class DummyBrightnessActivity extends Activity{

    private static final int DELAYED_MESSAGE = 1;

    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == DELAYED_MESSAGE) {
                    DummyBrightnessActivity.this.finish();
                }
                super.handleMessage(msg);
            }
        };
        Intent brightnessIntent = this.getIntent();
        float brightness = brightnessIntent.getFloatExtra("brightness value", 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        getWindow().setAttributes(lp);

        Message message = handler.obtainMessage(DELAYED_MESSAGE);
        //this next line is very important, you need to finish your activity with slight delay
        handler.sendMessageDelayed(message,1000); 
    }

}

这是将 Activity 添加到 AndroidManifest.xml 的方式,可能是最重要的部分:

<activity android:name="com.antonc.phone_schedule.DummyBrightnessActivity"
            android:taskAffinity="com.antonc.phone_schedule.Dummy"
            android:excludeFromRecents="true"
            android:theme="@style/EmptyActivity"></activity>

关于什么是什么的一些解释。

android:taskAffinity 必须与您的包名称不同!它使 DummyBrightnessActivity 不在您的主活动堆栈中启动,而是在单独的活动堆栈中启动,这意味着当 DummyBrightnessActivity 关闭时,您将看不到下一个活动,无论它是什么。在我添加此行之前,关闭 DummyBrightnessActivity 将调出我的主要活动。

android:excludeFromRecents="true" 使此活动在您明确想要的最近启动的应用程序列表中不可用。

android:theme="@style/EmptyActivity" 定义 DummyBrightnessActivity 对用户的外观,这就是您使其不可见的地方。这就是您在 styles.xml 文件中定义此样式的方式:

<style name="EmptyActivity" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Toast</item>
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">#00000000</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorForeground">#000</item>
</style>

这样您的 DummyBrightnessActivity 对用户来说将是不可见的。我不确定所有这些样式参数是否真的有必要,但它对我来说是这样。

我希望这能解释它,但如果您有任何疑问,请告诉我。

I've had the same problem of changing screen brightness from within a service, and a couple days ago i have successfully solved it(and updated my app Phone Schedule with brightness feature ;) ).
Ok, so this is the code you put into your service:

// This is important. In the next line 'brightness' 
// should be a float number between 0.0 and 1.0
int brightnessInt = (int)(brightness*255);

//Check that the brightness is not 0, which would effectively 
//switch off the screen, and we don't want that:
if(brightnessInt<1) {brightnessInt=1;}

// Set systemwide brightness setting. 
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightnessInt);

// Apply brightness by creating a dummy activity
Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("brightness value", brightness); 
getApplication().startActivity(intent);

Please Note that in the above code snippet I'm using two variables for brightness. One is brightness, which is a float number between 0.0 and 1.0, the other one is brightnessInt, which is an integer between 0 and 255. The reason for this is that Settings.System requires an integer to store system wide brightness value, while the lp.screenBrightness which you will see in the next code snippet requires a float. Don't ask me why not use the same value, this is just the way it is in Android SDK, so we're just going to have to live with it.

This is the code for DummyBrightnessActivity:

public class DummyBrightnessActivity extends Activity{

    private static final int DELAYED_MESSAGE = 1;

    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == DELAYED_MESSAGE) {
                    DummyBrightnessActivity.this.finish();
                }
                super.handleMessage(msg);
            }
        };
        Intent brightnessIntent = this.getIntent();
        float brightness = brightnessIntent.getFloatExtra("brightness value", 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        getWindow().setAttributes(lp);

        Message message = handler.obtainMessage(DELAYED_MESSAGE);
        //this next line is very important, you need to finish your activity with slight delay
        handler.sendMessageDelayed(message,1000); 
    }

}

This is how you add your activity to the AndroidManifest.xml, probably the most important part:

<activity android:name="com.antonc.phone_schedule.DummyBrightnessActivity"
            android:taskAffinity="com.antonc.phone_schedule.Dummy"
            android:excludeFromRecents="true"
            android:theme="@style/EmptyActivity"></activity>

A little explanation about what's what.

android:taskAffinity must be different, than your package name! It makes DummyBrightnessActivity be started not in your main stack of activities, but in a separate, which means that when DummyBrightnessActivity is closed, you won't see the next activity, whatever that may be. Until i included this line, closing DummyBrightnessActivity would bring up my main activity.

android:excludeFromRecents="true" makes this activity not available in the list of recently launched apps, which you definetely want.

android:theme="@style/EmptyActivity" defines the way DummyBrightnessActivity looks like to the user, and this is where you make it invisible. This is how you define this style in the styles.xml file:

<style name="EmptyActivity" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Toast</item>
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">#00000000</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorForeground">#000</item>
</style>

This way your DummyBrightnessActivity will be invisible to the user. I'm not shure if all of those style parameters are really necessary, but it works for me this way.

I hope that explains it, but if you have any questions, just let me know.

猫卆 2024-11-27 00:33:30

我刚到JB 登记。以下代码足以从服务

            android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt);

第一行设置亮度覆盖自动亮度。

I just checked in JB. The following code is enough to set brightness from a service

            android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt);

first line overrides the auto brightness.

第几種人 2024-11-27 00:33:30

您可能必须通过 Window 对象来完成此操作。

例如,

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

lp.screenBrightness= lp.screenBrightness*1.25;

getWindow().setAttributes(lp);

You might have to do it through the Window object.

For example,

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

lp.screenBrightness= lp.screenBrightness*1.25;

getWindow().setAttributes(lp);
不羁少年 2024-11-27 00:33:30

一种侵入性较小的方法(使用明显更少的代码)是:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.userActivity(SystemClock.uptimeMillis(), false);

A less intrusive way (using significantly less code) is:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.userActivity(SystemClock.uptimeMillis(), false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文