通过 Settings.System.putInt() 设置亮度不起作用

发布于 2024-10-09 09:09:07 字数 1287 浏览 0 评论 0原文

我正在尝试更改手机上的亮度模式和值,并发现 这里有很棒的帖子,我认为它回答了我的问题,但在我的 froyo Dell Streak 上,这段代码根本不执行任何操作。 putInt() 返回 true,所以从所有帐户看来它似乎是成功的,但亮度仍然完全相同,甚至无法让它在手动和自动之间切换......我有 WRITE_SETTINGS 权限集,并且 logcat似乎没有任何其他相关的输出出来......现在被难住了,要休息一会儿 o_0

这是我如何调用它的片段,这是在我的(唯一的)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....");
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

∞琼窗梦回ˉ 2024-10-16 09:09:07

这是 kotlin 为我工作的。

<使用权限
    android:name="android.permission.WRITE_SETTINGS"
    工具: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")
    }
}

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")
    }
}
╭⌒浅淡时光〆 2024-10-16 09:09:07

这对我有用:

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

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);
沉睡月亮 2024-10-16 09:09:07

完整的答案是:

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

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 );
        }
    }
沙与沫 2024-10-16 09:09:07

我还有一台运行 Froyo 的 Dell Streak。

屏幕亮度设置实际上对我来说确实发生了变化(如果您转到“设置”,您会看到滑块发生变化)。

然而,直到一段时间后,屏幕亮度实际上才会发生物理变化。这是我想不通的。

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.

又爬满兰若 2024-10-16 09:09:07

您的代码将更改亮度设置(存储的值),但这并不意味着它会更改当前亮度

如果您在服务中运行该代码,则无法更改当前亮度(除了奇怪的黑客之外)。如果代码在 UI 中运行,您可以更改亮度设置,然后更改当前亮度 使用窗口管理器

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文