DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: snarv
今日帖子: 12
在线用户: 15
导航: 论坛 -> 移动应用开发 斑竹:flyers,iamdream  
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/12 1:38:29
标题:
新手请问使用手电筒安卓权限问题 浏览:846
加入我的收藏
楼主: 我看台湾人写的教程几句代码就可以使用手电筒
此帖子包含附件:
PNG 图像
大小:99.3K
----------------------------------------------
-
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/12 1:40:18
1楼: 权限也是勾上的,但提示Required permission(s)[CAMERA]have not been granted.
此帖子包含附件:
PNG 图像
大小:158.6K
----------------------------------------------
-
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/12 1:42:55
2楼: 官方的示例就复杂很多,台湾人写的教程忽略了一些东西吗?还是必须要官方示例那样复杂呢?
//----------

// This software is Copyright (c) 2015 Embarcadero Technologies, Inc.
// You may only use this software if you are an authorized licensee
// of an Embarcadero developer tools product.
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.

//----------

unit FlashLightU;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Permissions,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, FMX.Effects,
  FMX.Objects, FMX.Layouts, FMX.Media;

type
  TFlashLightForm = class(TForm)
    FlashLight: TImage;
    ImageOn: TImage;
    FlashLightShadow: TShadowEffect;
    Light: TImage;
    ImageOff: TImage;
    ContainerLayout: TLayout;
    Camera: TCameraComponent;
    GlowEffect1: TGlowEffect;
    LayoutButtons: TLayout;
    procedure FormCreate(Sender: TObject);
    procedure ImageOffClick(Sender: TObject);
    procedure ImageOnClick(Sender: TObject);
  private
    FPermissionCamera: string;
    procedure SetFlashlightState(Active: Boolean);
    procedure AccessCameraPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
    procedure ActivateCameraPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
    procedure DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
  public
    { Public declarations }
  end;

var
  FlashLightForm: TFlashLightForm;

implementation

uses
{$IFDEF ANDROID}
  Androidapi.Helpers,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Os,
{$ENDIF}
  FMX.DialogService;

{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}

procedure TFlashLightForm.SetFlashlightState(Active: Boolean);
begin
  if Active then
    Camera.TorchMode := TTorchMode.ModeOn
  else
    Camera.TorchMode := TTorchMode.ModeOff;
end;

procedure TFlashLightForm.AccessCameraPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
begin
  // 1 permission involved: CAMERA
  if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
    ImageOff.Enabled := Camera.HasFlash
  else
    TDialogService.ShowMessage('Cannot access the camera flashlight because the required permission has not been granted');
end;

procedure TFlashLightForm.ActivateCameraPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
begin
  // 1 permission involved: CAMERA
  if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
  begin
    Camera.Active := True;
    ImageOff.Visible := False;
    ImageOn.Visible := True;
    SetFlashlightState(True);
    Light.Visible := True;
  end
  else
    TDialogService.ShowMessage('Cannot access the camera flashlight because the required permission has not been granted');
end;

// Optional rationale display routine to display permission requirement rationale to the user
procedure TFlashLightForm.DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
begin
  // Show an explanation to the user *asynchronously* - don't block this thread waiting for the user's response!
  // After the user sees the explanation, invoke the post-rationale routine to request the permissions
  TDialogService.ShowMessage('The app needs to access the camera in order to work',
    procedure(const AResult: TModalResult)
    begin
      APostRationaleProc;
    end)
end;

procedure TFlashLightForm.FormCreate(Sender: TObject);
begin
{$IFDEF ANDROID}
  FPermissionCamera := JStringToString(TJManifest_permission.JavaClass.CAMERA);
{$ENDIF}
  PermissionsService.RequestPermissions([FPermissionCamera], AccessCameraPermissionRequestResult, DisplayRationale);
end;

procedure TFlashLightForm.ImageOffClick(Sender: TObject);
begin
  PermissionsService.RequestPermissions([FPermissionCamera], ActivateCameraPermissionRequestResult, DisplayRationale);
end;

procedure TFlashLightForm.ImageOnClick(Sender: TObject);
begin
  ImageOff.Visible := True;
  ImageOn.Visible := False;
  SetFlashlightState(False);
  Light.Visible := False;
end;

end.
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/10/12 4:31:38
3楼: How ask permissions on Android

0) Project -> Options... Permission (Release/Debug)  check all that you needs
0.1) if necessary, you can ADD directly in Android.Manifest.Template.xml in your project folder, just use any text editor... if the does not exists, dont worry, just BUILD your project and it appears there

1) in code, same that the permissions does not needs user approval, you needs request it, using default way.

1.1) procedures to start...
procedure RequestPermissions(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);

.... for var i:integer := 0 to high(APermissions) do
......... xxxxxxxx any logic here xxxxxxxx


if AllOK then 
  .....  do your action here
else
  .... ERROR ERROR


procedure DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
    //
this procedure is not necessary at all, you can ignore it if does not needs show any msg to user.

2) you should run action ONLY all permissions was gived by user, else Dont.

3) you CAN NOT CHANGE any CAM property if
a) you dont have any permission
b) if CAM doing something - then, always stop firstly.


see my posts in WEDELPHI.COM 
http://wedelphi.com/t/422486/
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/12 12:43:47
4楼: @emailx45 Thank you. I have manually granted permissions to the app on my phone. The code does not report errors, but the flashlight cannot be turned on. What's going on? You must edit Android.Manifest.Template.xml or ask the user for these two methods. Choose one, are you indispensable without the other?
----------------------------------------------
-
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/12 15:24:54
5楼: @emailx45

CameraComponent1.Active := True;
//Add this sentence and the flashlight can be turned on, but still have to manually set permissions.

...\combobox\AndroidManifest.template.xml
...\combobox\Android\Debug\AndroidManifest.xml
...\combobox\Android\Debug\Project1\AndroidManifest.xml

There are three such files with different paths. Which one should be modified?
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/10/12 22:51:43
6楼: you ALWAYS works in "AndroidManifest.TEMPLATE.xml" in project folder ( except if you use another folder to store it, of course)!!!

when building your project, others files will be created and "deployed" with your app.

you need to do manually changes if necessary, else the RAD do it when you choice the options on menus.

if does not works, maybe this properties is not really used... then, needs investigate if is necessary use JAVA code directly, in this case.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/14 1:38:37
7楼: @emailx45 I tried and it's no use modifying 'AndroidManifest. TEMPLATE. xml'. You must use PermissionsService.RequestPermissions to get the user to agree
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/10/14 1:58:24
8楼: There are permissions that are strictly given by the system and do not require user authorization;
These permissions must be defined in AndroidManifestTemplate.xml (normally they do not require any code in your APP internally)

Ex.: BOOT_COMPLETED - need be in AndroidManifestTemplate.xml only
---- Why not in app code? --> because when bootting smartphone there is not user-UI!!!

There are permissions that require strict authorization from the user;
for these permissions, you will need to require permissions in AndroidManifestTemplate.xml and in the code;

Ex.: INTERNET - no need require on codigo, but needs be in AndroidManifestTemplate.xml file
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/14 2:15:30
9楼: But I have added permissions here.
此帖子包含附件:
PNG 图像
大小:239.4K
----------------------------------------------
-
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/14 2:16:25
10楼: why do I still get an error?
此帖子包含附件:
PNG 图像
大小:39.9K
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/10/14 2:41:42
11楼: to use your CAM in Android, basically:

0) check the permission in Project-Options... Permissions (CAMERA)
---- in "AndroidManifesTemplate.xml" it does not appears at all, because the IDE dont need!!!

---- this it will add in your "AndroidManifest.xml" when buiding your project -- see it after building your app in DEBUG or RELEASE folder!!!


1) in code, you need use:
  PermissionsService.RequestPermissions( { }
  [ LMyCAMPermission ],          { }
  prcPermissionsResulted,          { }
  prcDisplayRationale          { = nil, if you DONT WANT show any message! }
    );

LMyCAMPermission = JStringToString(TJManifest_permission.JavaClass.CAMERA); // android.permission.CAMERA

2) in code you needs:

    // RX11  procedure(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray)
    procedure prcPermissionsResulted(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);

    // RX11  procedure(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc)
    procedure prcDisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);

for test if user authorize or not CAM access!!!


procedure TfrmFormMain.prcPermissionsResulted(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
begin
  // verifying if the permissions was granted! - Here, testing only 1 permission = CAM
  if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then

    prcCAMStartCapture { execute your procedure here if all it's ok }

  else
    TDialogService.ShowMessage('The permission <<CAMERA access>> not allowed by user');
end;


4 ) any changes (actions) in CAM needs permission, for that you can:

    if PermissionsService.IsPermissionGranted(lMyCAMPermission) then
     .... CAM property changes ...


procedure TfrmFormMain.prcCAMStartCapture;
begin
  if not(lMyCAMDevice = nil) then
  begin
    // to Mobile (Android), change properties from CAMERA, needs permission!
{$IF DEFINED(ANDROID)}
    if PermissionsService.IsPermissionGranted(lMyCAMPermission) then
{$ENDIF}
    begin
      try
        lMyCAMDevice.StopCapture; // to avoid any error below
        //
        lMyCAMDevice.Quality := TVideoCaptureQuality.PhotoQuality;
        //
        lMyCAMDevice.StartCapture; // starting video capture!
        //
        prcMyLog('CAM device = Capture stated!');
        prcMyLog('CAM ' + fncMyIIF(lMyCAMDevice.IsDefault, 'is', 'is not') + ' Default');
        prcMyLog('CAM ' + fncMyIIF(lMyCAMDevice.HasFlash, 'has', 'has not') + ' Flash');

      except
        on E: Exception do
          prcMyLog('Error Start CAM' + #13#10 + E.Message);
      end;
    end
{$IF DEFINED(ANDROID)}
    else
      TDialogService.ShowMessage('Then CAM device needs your permission to access it!');
{$ENDIF}
  end
  else
    TDialogService.ShowMessage('None CAM device defined!');
end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/10/15 20:07:00
12楼: @emailx45 Thank you, I checked it out,Before Delphi 10.3, Android's authorization mode was one, which was determined during compilation. There was no need for users to dynamically authorize when the program was running. From 10.3 onwards, the authorization modes are divided into the following three types:

1 Normal Uses Permissions (no dynamic confirmation required)

2 Dangerous Uses Permissions (Dynamic confirmation required: confirmation when user program is running)

3 Signature Uses Permissions (No dynamic confirmation required)

The camera belongs to the second type.
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行132.8125毫秒 RSS