En prenant cette question de Stack Overflow Je me suis servi de cette unité d'aide, qui devrait fonctionner dans les versions de Delphi allant de XE8 à 10.1 Berlin, comme guide et qui semble faire l'affaire :
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.
Vous noterez que le code effectue la vérification des permissions via l'unité d'aide ci-dessous. Ce n'est pas vital tant que vous vous assurez d'avoir le fichier WRITE_SETTINGS
dans votre projet pour toutes les configurations de construction d'Android.
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.