1

I want to set the brightness of my android application only and not my phone. How do I change the brightness of my android application such that it does not effect my mobile phone brightness?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Janak
  • 607
  • 5
  • 23

2 Answers2

7

No need to give any permission only set Following Params in your Seekbar's Method

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
      // TODO Auto-generated method stub
      float BackLightValue = (float)arg1/100;
      BackLightSetting.setText(String.valueOf(BackLightValue)); // BackLignt is Textview to display value

      WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); // Get Params
      layoutParams.screenBrightness = BackLightValue; // Set Value
      getWindow().setAttributes(layoutParams); // Set params


     }
SANU
  • 192
  • 1
  • 8
  • when I close application and open again brightness is 0 or what ever I change is there. – Janak Sep 26 '14 at 11:27
  • Its always Start by given Default not 0 and if u want to start it by system then get System's Brightness and set to params int systemvalue= android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); – SANU Sep 26 '14 at 11:29
  • The code @SANU provided is Plagiarised from [http://android-er.blogspot.in/2011/02/change-system-screen-brightness-using.html](http://android-er.blogspot.in/2011/02/change-system-screen-brightness-using.html) and its manifest clearly has `` – Sagar Pilkhwal Sep 26 '14 at 11:44
  • @SANU: why do you think there is no permission required? Like android will just let you change the device's brightness? – Sagar Pilkhwal Sep 26 '14 at 11:45
  • 1
    @SagarPilkhwal If we want to change Whole System's brightness then we required permission otherwise not ...and this code is work for me – SANU Sep 29 '14 at 07:05
2

Get and Save the current brightness of your device, then change the brightness of the device(when your app starts running), and when your application closes revert back to the original brightness using the saved brightness level.

To Get Screen Brightness Level:

int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);

To Set Screen Brightness Level:

android.provider.Settings.System.putInt(getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, value); //<-- 1-225

App manifest permissions:

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

or

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = curBrightnessValue/100.0f; //<-- your value here
getWindow().setAttributes(layoutParams);

here is a tutorial link

Another SO Post Link

P.S: you will have to handle all events like onPause(), onResume(), onBackPressed() etc

Community
  • 1
  • 1
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77