更改画面亮度系统设定的Andr​​oid [英] Changing the Screen Brightness System Setting Android

查看:193
本文介绍了更改画面亮度系统设定的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从withing服务改变屏幕亮度,像这样:

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);

问题是,是行不通的。嗯,其实它成功地改变亮度设置,但屏幕亮度不会真正改变,直到我进入手机设置,看一下新的值,点击OK。

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?

推荐答案

我已经从一个服务中改变屏幕的亮度相同的问题,前两天我已经成功地解决了它(和更新我的应用程序<一HREF =htt​​ps://play.google.com/store/apps/details?id=com.antonc.phone_schedule>手机日程表亮度功能;))。 好了,这是code,你把你的服务:

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);

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

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.

这是在$ C $下DummyBrightnessActivity:

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); 
    }

}

这是你怎么加的活动到AndroidManifest.xml中,可能是最重要的部分:

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.

安卓taskAffinity 必须是不同的,比你的包名!它使DummyBrightnessActivity在活动的主栈没有开始,但在一个单独的,这意味着当DummyBrightnessActivity是封闭的,你不会看到下一个活动,无论可能。直到我包括这条线,收盘DummyBrightnessActivity会带来我的主要活动。

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.

机器人:excludeFromRecents =真正的使这个在最近推出的应用程序,您definetely希望列表不可用活性

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

安卓主题=@风格/ EmptyActivity定义DummyBrightnessActivity貌似给用户的方式,而这正是你做不可见。这就是你在styles.xml文件中定义这种风格:

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>

这样你的DummyBrightnessActivity将是用户看不到的。我不是舒尔如果所有这些样式参数是确有必要的,但它为我这样的。

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.

这篇关于更改画面亮度系统设定的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆