2

I'm trying to change the brightness mode and value on my phone, and found a great thread here at SO that I thought answered my question, but on my froyo Dell Streak this code simply does nothing. putInt() is returning true so by all accounts it seems to be successful, yet brightness remains exactly the same, can't even get it to switch between manual and automatic......I have the WRITE_SETTINGS permission set, and logcat doesn't seem to have any other relevant output coming out....stumped right now, gonna take a break for a while o_0

Here's a snippet of how I'm calling it, this is inside my (only) Activity:

public void onCheckedChanged(RadioGroup group, int checked) {
    //BrightSettings bright = new BrightSettings();

    switch(checked)
    {
        case R.id.daybutton:
            //bright.setBrightMode(BrightSettings.DAY_MODE);
            if(Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC))
                Log.d("NightMode", "Brightness seems to be set to auto....");
        case R.id.nightbutton:
            //bright.setBrightMode(BrightSettings.NIGHT_MODE);
            if (Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) && Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 2) )
                Log.d("NightMode", "Night mode seems to have been set....");
    }
}
Community
  • 1
  • 1
jamzsabb
  • 1,125
  • 2
  • 18
  • 40
  • Do you have the `MODIFY_PHONE_STATE` permission? I would check that first. – Sebastian Roth Dec 30 '10 at 05:58
  • I don't see how MODIFY_PHONE_STATE is relevant, it controls the telephony state, but I gave that a shot and it still gave me nothing. I'm really confused because putInt() returns true if successful, and I'm getting the debug message indicating the if statement was true so it's just weird. Thanks for the suggestion though – jamzsabb Dec 31 '10 at 23:16
  • I have the same problem. Some Devices like a Galaxy Tab will reread and apply the settings automatically. Other devices will only apply the settings after the srcreen was locked or the device rebooted. – Janusz Feb 08 '11 at 15:19

5 Answers5

2

It's work for me by kotlin。

<uses-permission
    android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" />
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_test)
    if(isCanWriteSettings(this))
        setupLight(this,255)//0~255
    else
        requestCanWriteSettings(this)
}
/**
 *
 * @param context
 * @param light max 255
 */
fun setupLight(context: Context, light: Int) {
    try {
        val brightnessMode = Settings.System.getInt(
            context.contentResolver,
            Settings.System.SCREEN_BRIGHTNESS_MODE
        )
        if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
            Settings.System.putInt(
                context.contentResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
            )
        }
        Settings.System.putInt(
            context.contentResolver,
            Settings.System.SCREEN_BRIGHTNESS,
            light
        )
    } catch (e: Exception) {
        Log.e("setupLight","Exception $e")
    }
}

fun isCanWriteSettings(context: Context): Boolean {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.System.canWrite(context)
}


fun requestCanWriteSettings(activity: Activity){
    if (isCanWriteSettings(context = activity))
        return //not need
    try {
        val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
        intent.data = Uri.parse("package:" + activity.packageName)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        activity.startActivityForResult(intent, 0)
    }catch (e: Exception){
        Log.e("requestCanWriteSettings","requestCanWriteSettings $e")
    }
}
Deyu瑜
  • 484
  • 4
  • 14
1

This works for me:

int brightnessMode = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE);

if (brightnessMode == android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
{
    android.provider.Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0);
Rizwan Asif
  • 125
  • 1
  • 8
1

A complete Answer would be:

 /**
     * Set Device Brightness
     *
     * @param brightness Range (0-255)
     */
    @Override
    public void setDeviceBrightness(int brightness) {

        if (!Settings.System.canWrite(this.context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + this.context.getPackageName()));
            this.context.startActivity(intent);
            return;
        }

        try {
            if (Settings.System.getInt(
                     this.context.contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                Settings.System.putInt(
                          this.context.contentResolver,
                        Settings.System.SCREEN_BRIGHTNESS_MODE,
                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            }
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        if(Settings.System.putInt(this.context.contentResolver, SCREEN_BRIGHTNESS, brightness )) {
            Log.i("Updated System brightness to: " + brightness );
        }else {
            Log.w("ERROR: Failed to Updated System brightness to: " + brightness );
        }
    }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

Your code will change brightness setting (the stored value), but that doesn't mean it'll change current brightness.

If you're running that code in a service, you have no way to change current brightness (aside strange hacks). If the code is running in the UI, you can change brightness settings and then change current brightness using WindowManager.

Community
  • 1
  • 1
Diego
  • 5,326
  • 1
  • 35
  • 32
0

I also have a Dell Streak running Froyo.

The screen brightness setting actually DOES change for me (if you go to Settings you'll see the slider change).

However, the screen brightness does not actually physically change until after some time. This is what I can't figure out.

mellowg
  • 1,786
  • 3
  • 15
  • 19