Java教程

delphi给App授予权限

本文主要是介绍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:

 

 


  

这篇关于delphi给App授予权限的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!