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

时间:2010-12-30 05:55:01

标签: android settings brightness

我正在尝试更改手机上的亮度模式和值,并找到了great thread here at 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....");
    }
}

5 个答案:

答案 0 :(得分:1)

科特林为我工作。

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

答案 1 :(得分:0)

我还有戴斯Streak运行Froyo。

屏幕亮度设置实际上对我有所改变(如果你转到设置,你会看到滑块更改)。

然而,直到一段时间后,屏幕亮度才会实际发生物理变化。这是我无法弄清楚的。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

这对我有用:

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

答案 4 :(得分:0)

一个完整的答案是:

Errore: The following error occured when compiling and adapting the model using rjags:
 Error in rjags::jags.model(model, data = dataenv, n.chains = length(runjags.object$end.state),  : 
  RUNTIME ERROR:
Compilation error on line 12.
Index out of range taking subset of  alpha