以编程方式更改 6.0 中的屏幕亮度
Changing Screen brightness in 6.0 programmatically
我正在开发一个 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);
}
显然您需要明确询问用户对设备的许可 运行 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 权限。
如果您想要指定 activity/fragment,试试这个:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 70 / 100.0f;
getWindow().setAttributes(lp);
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")
}
}
我正在开发一个 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);
}
显然您需要明确询问用户对设备的许可 运行 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 权限。
如果您想要指定 activity/fragment,试试这个:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 70 / 100.0f;
getWindow().setAttributes(lp);
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")
}
}