4

In my Android application I am using the following code to change screen brightness

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

On Android devices it works fine, but it doesn't work when I port it to BlackBerry-10. Is there a different method I should use or a workaround?


Torcellite's solution (by Anton Cherkashyn):

I tried this, after further testing it does change brightness on Android device, but not on BlackBerry. The only reason why it is dimming on BlakcBerry is because of opening new activity (activity change dim).

In my activity:

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

Dummy:

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

Manifest:

 <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>
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57

1 Answers1

0

While the code you're using only changes the brightness of the current screen, the code I'm going to post changes the system brightness.

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

Where bright is an integer value from 0-255.

This method will not update your screen's brightness though. So, you'll need to call an invisible activity and close it to update the screen brightness.

Here's more info.

[EDIT]

Try adding this to your manifest:

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

[EDIT]

Try adding this too :

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
Community
  • 1
  • 1
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
  • Thank you for your response. I tried to use this part of your code, it does not work on BlackBerry (brigness won't change at all!). The code from the link however does make a brightness change (always down, even if it should make it brighter), but after less then a second it resets back to what it used to be. – Bojan Kogoj Jan 22 '13 at 16:58
  • Like I said before, the code you're using will change the brightness of only the screen. Try putting my code in after `getWindow().setAttributes(lp);` – Karthik Balakrishnan Jan 23 '13 at 01:54
  • It doesn't work, still does the same. My guess is Android app isn't allowed to change phone brightness settings. – Bojan Kogoj Jan 23 '13 at 17:51
  • I guess. But it doesn't make any sense. If they're going to be converting Android apps, they'd allow brightness. Have you tried pasting my part of the code in your main activity? – Karthik Balakrishnan Jan 23 '13 at 17:55
  • Well settings are supported, permission isn't among unsupported (http://ow.ly/h4b1C). But it still doesn't work, i tried putting it everywhere. – Bojan Kogoj Jan 23 '13 at 18:03