delphi 暗屏Android Firemonkey

cnjp1d6j  于 2023-04-05  发布在  Android
关注(0)|答案(1)|浏览(169)

有人知道如何在Android的 Delphi Firemonkey上调暗屏幕吗?我一直在谷歌上搜索,但只能找到如何通过获取Wakelock(Delphi XE5 Android. How to use PowerManager.WakeLock?)来删除Android的自动调暗。
我最多想达到两种状态:晚上= 20%亮度白天= 100%亮度
我用的是 Delphi 10.1柏林。谢谢你的帮助。

4sup72z8

4sup72z81#

以具有Java解决方案的this Stack Overflow question为指导,我匆忙地找到了这个助手单元,它应该可以在从XE8到10.1柏林的 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.

你会注意到代码通过下面的helper单元进行权限检查。这并不重要,只要你确保你的项目中所有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.

相关问题