delphi11安卓权限的改变

delphi11安卓权限的改变

/// <author>cxg 2022-12-21</author>

unit uRights;

interface

uses
  System.Permissions,
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs
  , UI.Frame, UI.Standard, UI.Base
  ;

type
  TRights = class(TFrame)
    LinearLayout1: TLinearLayout;
    btnBack: TTextView;
    tvTitle: TTextView;
    ButtonView1: TButtonView;
    procedure btnBackClick(Sender: TObject);
    procedure ButtonView1Click(Sender: TObject);
  private
    { Private declarations }
    FPermissionCamera,
    FPermissionReadExternalStorage,
    FPermissionWriteExternalStorage: string;
    {$IF CompilerVersion >= 35.0}
    procedure TakePicturePermissionRequestResult(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
    procedure DisplayRationale(const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
    {$ELSE}
    procedure TakePicturePermissionRequestResult(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
    procedure DisplayRationale(const APermissions: TArray<string>; const APostRationaleProc: TProc);
    {$ENDIF}
  public
    { Public declarations }
    procedure DoCreate; override;
  end;

var
  Rights: TRights;

implementation

{$R *.fmx}

uses
  Androidapi.Jni.Os,
  Androidapi.Helpers;

procedure TRights.btnBackClick(Sender: TObject);
begin
  Finish;
end;

procedure TRights.ButtonView1Click(Sender: TObject);
begin
  FPermissionCamera := JStringToString(TJManifest_permission.JavaClass.CAMERA);
  FPermissionReadExternalStorage := JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
  FPermissionWriteExternalStorage := JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE);
  PermissionsService.RequestPermissions([FPermissionCamera, FPermissionReadExternalStorage, FPermissionWriteExternalStorage], TakePicturePermissionRequestResult, DisplayRationale);
end;

{$IF CompilerVersion >= 35.0}
procedure TRights.DisplayRationale(const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
var
  I: Integer;
  RationaleMsg: string;
begin
  for I := 0 to High(APermissions) do
  begin
    if APermissions[I] = FPermissionCamera then
      RationaleMsg := RationaleMsg + '应用需要拍照' + SLineBreak + SLineBreak
    else if APermissions[I] = FPermissionReadExternalStorage then
      RationaleMsg := RationaleMsg + '应用需要读取照片';
  end;


  APostRationaleProc;
  ShowMessage(RationaleMsg);
end;
{$ELSE}
procedure TRights.DisplayRationale(Sender: TObject;
  const APermissions: TArray<string>; const APostRationaleProc: TProc);
var
  I: Integer;
  RationaleMsg: string;
begin
  for I := 0 to High(APermissions) do
  begin
    if APermissions[I] = FPermissionCamera then
      RationaleMsg := RationaleMsg + '应用需要拍照' + SLineBreak + SLineBreak
    else if APermissions[I] = FPermissionReadExternalStorage then
      RationaleMsg := RationaleMsg + '应用需要读取照片';
  end;


  APostRationaleProc;
  ShowMessage(RationaleMsg);
end;
{$ENDIF}

procedure TRights.DoCreate;
begin

end;

{$IF CompilerVersion >= 35.0}
procedure TRights.TakePicturePermissionRequestResult(
  const APermissions: TClassicStringDynArray;
  const AGrantResults: TClassicPermissionStatusDynArray);
begin
 // 申请的3个权限: CAMERA, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE
  if (Length(AGrantResults) = 3) and (AGrantResults[0] = TPermissionStatus.Granted) and (AGrantResults[1] = TPermissionStatus.Granted) and (AGrantResults[2] = TPermissionStatus.Granted) then
  begin
    //权限被用户允许的代码
  end
  else
    //权限被用户禁止的代码
    ShowMessage('权限禁止,不能拍照.');
end;
{$ELSE}
procedure TRights.TakePicturePermissionRequestResult(
  const APermissions: TArray<string>;
  const AGrantResults: TArray<TPermissionStatus>);
begin
 // 申请的3个权限: CAMERA, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE
  if (Length(AGrantResults) = 3) and (AGrantResults[0] = TPermissionStatus.Granted) and (AGrantResults[1] = TPermissionStatus.Granted) and (AGrantResults[2] = TPermissionStatus.Granted) then
  begin
    //权限被用户允许的代码
  end
  else
    //权限被用户禁止的代码
    ShowMessage('权限禁止,不能拍照.');
end;
{$ENDIF}

end.

 

posted @ 2022-12-21 08:51  delphi中间件  阅读(410)  评论(0编辑  收藏  举报