delphi给App授予权限

首先要切换到安卓平台下:

 

 

第一步:

  Project — Options — Application — Uses Permissions:

 

  •  注意如下图:产生费用的功能 要谨慎勾选——打电话、发送短信…

 

 

第二步:

引用权限单元UnitPermission:(Alt+F11)

unit UnitPermission;

interface

uses
{$IFDEF ANDROID} Androidapi.Helpers,
 Androidapi.JNI.JavaTypes, Androidapi.JNI.OS, {$ENDIF}
  System.Permissions;

function RequestLocation: Boolean;
function RequestRecordAudio: Boolean;
function RequestCamera: Boolean;
function RequestReadStorage: Boolean;
function RequestWriteStorage: Boolean;
function RequestSendSMS:Boolean;
function RequestReadPhoneState:Boolean;

implementation

function RequestReadPhoneState: Boolean;
var
  Value: Boolean;
begin
  Value := False;

{$IFDEF ANDROID}
  PermissionsService.RequestPermissions(
  [JStringToString(TJManifest_permission.JavaClass.READ_PHONE_STATE)],
    procedure(const APermissions: TArray<string>;
     const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;
end;


function RequestLocation: Boolean;
var
  Value: Boolean;
begin
  Value := False;

{$IFDEF ANDROID}
  PermissionsService.RequestPermissions(
  [JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION)],
    procedure(const APermissions: TArray<string>;
     const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;
end;

function RequestRecordAudio: Boolean;
var
  Value: Boolean;
begin
  Value := False;
{$IFDEF ANDROID}
  PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.RECORD_AUDIO)],
    procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;
end;

function RequestCamera: Boolean;
var
  Value: Boolean;
begin
  Value := False;
{$IFDEF ANDROID}
  PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.CAMERA)],
    procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;

end;

function RequestReadStorage: Boolean;
var
  Value: Boolean;
begin
  Value := False;
{$IFDEF ANDROID}
  PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE)],
    procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;
end;

function RequestWriteStorage: Boolean;
var
  Value: Boolean;
begin
  Value := False;
{$IFDEF ANDROID}
  PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE)],
    procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;
end;

function RequestSendSMS:Boolean;
var
  Value: Boolean;
begin
  Value := False;
{$IFDEF ANDROID}
  PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.SEND_SMS)],
    procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
    begin
      if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
      begin
        Value := (True);
      end
      else
      begin
        Value := (False);
      end;
    end);
{$ENDIF}
  Result := Value;
end;

end.

 

更改授权功能,对下图 红框中的 进行更改,还有其他一些小地方自己更改下就可以了

 

 把空格的地方 换作 下划线_ ;其他功能都类似:

 

 

最后注意:

如果按照上述方法 授权,功能仍不可用的话, 尝试勾选 下面这个
Project——Options——Application——Entitlement List:

 

 


  

posted @ 2021-12-21 22:56  麦麦提敏  阅读(488)  评论(0编辑  收藏  举报