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 xe安装2010版控件

Delphi system 提供的编译期函数

Delphi combobox的属性和事件、及几个鼠标事件的触发

Delphi中用tlistview显示数据库数据

Delphi 获取打开的记事本中的内容

Delphi 通过进程名获得文件全路径的函数

Delphi的枚举类型

Delphi 调用golang dll

Delphi中获取memo鼠标所在位置的行和列

Delphi剪切板-监视剪贴板

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



打赏

取消

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

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

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

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

评论

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