gpt4 book ai didi

android - 昏暗的屏幕 Android Firemonkey

转载 作者:太空狗 更新时间:2023-10-29 13:11:51 25 4
gpt4 key购买 nike

有谁知道如何在 Delphi Firemonkey for Android 上调暗屏幕?我一直在谷歌搜索和搜索,但只能找到如何通过获取 Wakelock ( Delphi XE5 Android. How to use PowerManager.WakeLock? ) 来删除 android 的自动暗淡。

充其量我想达到两种状态:晚上 = 20% 亮度白天 = 100% 亮度

我使用的是 Delphi 10.1 Berlin。感谢您的帮助。

最佳答案

服用 this Stack Overflow question ,它有 Java 解决方案,作为指南,我翻出了这个辅助单元,它应该在从 XE8 到 10.1 Berlin 左右的 Delphi 版本中工作,这似乎可以解决问题:

unit ScreenBrightnessU;

interface

function GetScreenBrightness: Byte;
procedure SetScreenBrightness(Brightness: Byte);

implementation

uses
MiscU,
FMX.Helpers.Android,
{$IF RTLVersion >= 31}
FMX.DialogService,
{$ELSE}
FMX.Dialogs,
{$ENDIF}
System.UITypes,
System.SysUtils,
Androidapi.Helpers,
Androidapi.JNI.App,
Androidapi.JNI.Provider,
Androidapi.JNI.GraphicsContentViewText;

function GetScreenBrightness: Byte;
var
Resolver: JContentResolver;
begin
Resolver :=
{$IF RTLVersion >= 30}
TAndroidHelper.ContentResolver;
{$ELSE}
SharedActivityContext.getContentResolver;
{$ENDIF}
Result := TJSettings_System.JavaClass.getInt(
Resolver,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS);
end;

procedure SetScreenBrightness(Brightness: Byte);
var
Resolver: JContentResolver;
AttainedBrightness: Single;
LayoutParams: JWindowManager_LayoutParams;
Window: JWindow;
begin
if not HasPermission('android.permission.WRITE_SETTINGS') then
{$IF RTLVersion >= 31}
TDialogService.MessageDialog('App does not have the WRITE_SETTINGS permission', TMsgDlgType.mtError, [TMsgDlgBtn.mbCancel], TMsgDlgBtn.mbCancel, 0, nil)
{$ELSE}
MessageDlg('App does not have the WRITE_SETTINGS permission', TMsgDlgType.mtError, [TMsgDlgBtn.mbCancel], 0)
{$ENDIF}
else
begin
{$IF RTLVersion >= 30}
Resolver := TAndroidHelper.ContentResolver;
{$ELSE}
Resolver := SharedActivityContext.getContentResolver;
{$ENDIF}
// This will set the manual mode (set the automatic mode off)
TJSettings_System.JavaClass.putInt(
Resolver,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS_MODE,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS_MODE_MANUAL);
// This will set the required brightness
TJSettings_System.JavaClass.putInt(
Resolver,
TJSettings_System.JavaClass.SCREEN_BRIGHTNESS,
Brightness);
try
AttainedBrightness := GetScreenBrightness;
CallInUIThread(
procedure
begin
{$IF RTLVersion >= 30}
Window := TAndroidHelper.Activity.getWindow;
{$ELSE}
Window := SharedActivity.getWindow;
{$ENDIF}
LayoutParams := Window.getAttributes;
LayoutParams.screenBrightness := AttainedBrightness / 255;
Window.setAttributes(LayoutParams);
end);
except
// ONOES!!!!
// <sweeps issue under the carpet>
end;
end;
end;

end.

您会注意到代码通过下面的辅助单元进行权限检查。这并不重要,只要您确保在您的项目中为所有 Android 构建配置设置了 WRITE_SETTINGS 权限即可。

unit MiscU;

interface

function HasPermission(const Permission: string): Boolean;

implementation

uses
FMX.Helpers.Android,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText;

function HasPermission(const Permission: string): Boolean;
begin
//Permissions listed at http://d.android.com/reference/android/Manifest.permission.html
{$IF RTLVersion >= 30}
Result := TAndroidHelper.Context.checkCallingOrSelfPermission(
{$ELSE}
Result := SharedActivityContext.checkCallingOrSelfPermission(
{$ENDIF}
StringToJString(Permission)) =
TJPackageManager.JavaClass.PERMISSION_GRANTED
end;

end.

关于android - 昏暗的屏幕 Android Firemonkey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39640515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com