在6.0中编程更改屏幕亮度

3

我正在开发一个可以编程控制屏幕亮度的Android应用程序。我有一种可行的设置亮度的方法适用于5.1.1版本,但是当我在6.0版本中运行该应用程序时,它会出现错误并关闭应用程序。请帮忙。

以下是我的方法:

 public void setBrightness(View view,int brightness){

    //Just a loop for checking whether the brightness changes
     if(brightness <46)
        brightness = 255;
    else if(brightness >150)
        brightness=45;
    ContentResolver cResolver = this.getApplicationContext().getContentResolver();
    Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);

}

public void setDisplay(View view)
{
    ContentResolver  cResolver = getContentResolver();

    Window window = getWindow();
    int brightness=0;
    try
    {        Settings.System.putInt(cResolver,Settings.System.SCREEN_BRIGHTNESS_MODE
                                   Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                   brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
        System.out.println("Current Brightness level " + brightness);
    }
    catch (Exception e)
    {
                   Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }
    setBrightness(view,brightness);
}

尝试这个:https://dev59.com/oWMl5IYBdhLWcg3wingW - Jhaman Das
6.0 版本需要我额外做些什么吗? - Shashank
检查运行时权限 - Jhaman Das
你在清单文件中设置了<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>这个权限吗? - Jhaman Das
我已经在清单文件中包含了这个权限。 - Shashank
显示剩余3条评论
3个回答

15

如果您想要针对特定的活动/片段进行操作,请尝试以下方法:

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = 70 / 100.0f;
    getWindow().setAttributes(lp);

4
这是在你的应用程序中仅针对片段或活动设置亮度的答案。 - elliptic1
1
谢谢!这节省了我很多时间。 - Kimble

10

显然,在运行Android 6.0+的设备上,您需要明确要求用户的许可。

尝试以下代码:(我已为您修改)

@TargetApi(Build.VERSION_CODES.M)
public void setBrightness(View view,int brightness){

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.System.canWrite(getApplicationContext()))
        {
            if (brightness < 46)
                brightness = 255;
            else if (brightness > 47)
                brightness = 0;

            ContentResolver cResolver = this.getApplicationContext().getContentResolver();
            Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);

        } else

        {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + this.getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
}

请注意你的清单中需要加入WRITE_SETTINGS权限。


1
感谢您的回答,@TargetApi(Build.VERSION_CODES.M) 应该实际上放在里面。 - Shashank

2

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

工具很赞:ignore="ProtectedPermissions"。 - Junaed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接