更改屏幕亮度系统设置Android

Kra*_*atz 17 settings service android screen-brightness

我试图通过服务来改变屏幕亮度,如下所示:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);
Run Code Online (Sandbox Code Playgroud)

问题是那是行不通的.嗯,实际上它成功地改变了亮度设置,但是在我进入手机设置之前屏幕亮度实际上并没有改变,看看新值并点击确定.

设置值后我是否需要做些什么来改变亮度?

Ant*_*hyn 39

我有同样的问题,从服务中改变屏幕亮度,几天前我已成功解决它(并更新我的应用程序电话时间表与亮度功能;)).好的,这是您在服务中添加的代码:

// 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);
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的代码片段中,我使用两个变量来表示亮度.一个是brightness,它是一个介于0.0和1.0之间的浮点数,另一个是brightnessInt0到255之间的整数.原因是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); 
    }

}
Run Code Online (Sandbox Code Playgroud)

这是您将活动添加到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>
Run Code Online (Sandbox Code Playgroud)

关于什么是什么的一点解释.

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>
Run Code Online (Sandbox Code Playgroud)

这样,您的DummyBrightnessActivity将对用户不可见.如果所有这些样式参数都非常必要,我并不感到害羞,但这种方式对我有用.

我希望能解释清楚,但如果您有任何疑问,请告诉我.

  • 效果很好.我对样式`EmptyActivity` <style name ="EmptyActivity"parent ="android:Theme.Dialog"> ....进行了一些小改动.(与Anton Cherkashyn发布的相同)<item name ="android:windowBackground"> @android:color/transparent </ item> <item name ="android:backgroundDimEnabled"> false </ item> <item name ="android:windowIsFloating"> true </ item> </ style> (3认同)

use*_*769 6

我刚刚检查了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);
Run Code Online (Sandbox Code Playgroud)

第一行会覆盖自动亮度.