2

我正在尝试更改手机上的亮度模式和值,并在 SO 找到了一个很棒的线程,我认为它回答了我的问题,但在我的 froyo Dell Streak 上,此代码根本没有任何作用。putInt() 正在返回 true,因此所有帐户似乎都成功了,但亮度保持不变,甚至无法在手动和自动之间切换……我设置了 WRITE_SETTINGS 权限和 logcat似乎没有任何其他相关输出出现....现在被难住了,要休息一会儿o_0

这是我如何称呼它的片段,这是在我的(唯一)活动中:

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

5 回答 5

1

这是 kotlin 为我工作的。</p>

<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")
    }
}
于 2020-02-18T09:28:35.240 回答
1

这对我有用:

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);
于 2018-01-10T14:00:15.543 回答
0

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

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

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

于 2011-06-24T04:46:42.947 回答
0

一个完整的答案是:

 /**
     * 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 );
        }
    }
于 2021-01-13T19:20:41.400 回答
0

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

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

于 2015-12-02T16:19:36.027 回答