Delphi 安卓 APP 动态权限申请


本文整理自网络,侵删。

 

这里头的例子代码,亲测有效。放一个 TSwitch 上去,拨动开关,触发代码,Android 8 的手机,会弹出 APP 申请权限的界面让用户选择是否给与 APP 权限。

这里要注意的问题是,上述代码在编译的时候,需要 uses 几个库:

1.  System.Permissions

2.  {$IFDEF ANDROID},
Androidapi.JNI.JavaTypes,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Location,
  Androidapi.JNIBridge,
  Androidapi.JNI.Os,
  Androidapi.Helpers,
  Androidapi.JNI.Provider;
{$ENDIF};
--------------------- 

 

procedure TLocationForm.swLocationSensorActiveSwitch(Sender: TObject);
begin
{$IFDEF ANDROID}
  if swLocationSensorActive.IsChecked then
    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
          { activate or deactivate the location sensor }
          LocationSensor1.Active := True
        else
        begin
          swLocationSensorActive.IsChecked := False;
          TDialogService.ShowMessage('Location permission not granted');
        end;
      end)
  else
{$ENDIF}
    LocationSensor1.Active := False;
end;

相关阅读 >>

Delphi 判断一目录是否共享

Delphi msxml 获取 api接口

ring3下清零结束进程的Delphi版本

Delphi 极速字符串替换函数

dbgrid导出excl表

Delphi 动态数组的使用

Delphi 检测链接和获取链接内容

Delphi superobject 序列数据集

Delphi adoquery查询,如何得到查询记录数?

Delphi实现md5算法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...