Android 在6.0中以编程方式更改屏幕亮度
我正在开发一个android应用程序,通过编程改变屏幕亮度。我有一个在5.1.1上运行的设置亮度方法,但是当我在6.0上运行应用程序时,它会给出一个错误并关闭应用程序。 请帮忙 我的做法如下:Android 在6.0中以编程方式更改屏幕亮度,android,android-6.0-marshmallow,screen-brightness,Android,Android 6.0 Marshmallow,Screen Brightness,我正在开发一个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) b
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);
}
public void亮度(视图,整数亮度){
//只是一个用来检查亮度是否改变的循环
中频(亮度150)
亮度=45;
ContentResolver cResolver=this.getApplicationContext().getContentResolver();
Settings.System.putInt(cResolver,Settings.System.SCREEN\u亮度,亮度);
}
公共void setDisplay(视图)
{
ContentResolver cResolver=getContentResolver();
Window=getWindow();
int亮度=0;
尝试
{Settings.System.putInt(cResolver,Settings.System.SCREEN\u亮度\u模式
设置、系统、屏幕(亮度、模式、手动);
亮度=Settings.System.getInt(cResolver,Settings.System.SCREEN\u亮度);
System.out.println(“当前亮度级别”+亮度);
}
捕获(例外e)
{
Log.e(“错误”,“无法访问系统亮度”);
e、 printStackTrace();
}
清晰度(视野、亮度);
}
显然,您需要明确请求用户对运行Android 6.0+的设备的权限
尝试以下代码:(我已经为您修改了它)
@TargetApi(Build.VERSION\u CODES.M)
公共空间亮度(视图、整数亮度){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
if(Settings.System.canWrite(getApplicationContext()))
{
如果(亮度<46)
亮度=255;
否则如果(亮度>47)
亮度=0;
ContentResolver cResolver=this.getApplicationContext().getContentResolver();
Settings.System.putInt(cResolver,Settings.System.SCREEN\u亮度,亮度);
}否则
{
Intent Intent=新意图(android.provider.Settings.ACTION\u MANAGE\u WRITE\u Settings);
setData(Uri.parse(“包:”+this.getPackageName());
intent.addFlags(intent.FLAG\u活动\u新任务);
星触觉(意向);
}
}
请注意,您还需要清单中的“写入设置”权限。如果要指定活动/片段,请尝试以下操作:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 70 / 100.0f;
getWindow().setAttributes(lp);
科特林版本
override-fun-onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u测试)
如果(isCanWriteSettings(此))
setupLight(this,255)//0~255
其他的
requestCanWriteSettings(此)
}
/**
*
*@param上下文
*@param light max 255
*/
趣味设置灯光(上下文:上下文,灯光:Int){
试一试{
val brightnessMode=Settings.System.getInt(
context.contentResolver,
设置.System.SCREEN\u亮度\u模式
)
if(亮度模式==设置.系统.屏幕亮度模式自动){
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_亮度_模式,
设置.系统.屏幕\亮度\模式\手册
)
}
Settings.System.putInt(
context.contentResolver,
设置.System.SCREEN_亮度,
光
)
}捕获(e:例外){
Log.e(“setupLight”,“异常$e”)
}
}
有趣的isCanWriteSettings(上下文:上下文):布尔值{
返回Build.VERSION.SDK_INT
试试这个6.0还有什么我需要做的吗?请检查运行时许可证,我是初学者,你在清单中设置了这个许可证了吗?谢谢你的回答@TargetApi(Build.VERSION\u CODES.M)实际上应该在内部。这是仅在应用程序中设置片段或活动的亮度的答案。谢谢!这为我节省了很多时间:ignore=“ProtectedPermissions”
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 70 / 100.0f;
getWindow().setAttributes(lp);
<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")
}
}