从Android应用程序更改BlackBerry-10屏幕亮度

时间:2013-01-08 23:59:57

标签: android blackberry-10 screen-brightness blackberry-android

在我的Android应用程序中,我使用以下代码来更改屏幕亮度

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = someValue;
getWindow().setAttributes(lp);

在Android设备上它工作正常,但当我将其移植到BlackBerry-10时它不起作用。我应该使用不同的方法或解决方法吗?


Torcellite的解决方案(Anton Cherkashyn):

我试过这个,经过进一步的测试后,确实会改变Android设备的亮度,但不会改变BlackBerry的亮度。它在BlakcBerry上变暗的唯一原因是因为开启了新活动(活动变化暗淡)。

在我的活动中:

Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("brightness value", value[screenBrightness]); 
getApplication().startActivity(intent);

虚拟:

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 android:name=".DummyBrightnessActivity"
            android:taskAffinity=".Dummy"
            android:excludeFromRecents="true"
            android:theme="@style/EmptyActivity"></activity>

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">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorForeground">#000</item>
    </style>

1 个答案:

答案 0 :(得分:0)

虽然您使用的代码只会改变当前屏幕的亮度,但我要发布的代码会改变系统亮度。

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

其中bright是0-255之间的整数值。

此方法不会更新屏幕的亮度。因此,您需要调用不可见的活动并关闭它以更新屏幕亮度。

这里有更多info

[编辑]

尝试将此添加到您的清单中:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

[编辑]

尝试添加此内容:

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />