Since Android 6.0 Marshmallow, users can control the permissions needed by an application. And developers should maintain the accessibility of their applications by handling these permissions properly.
In this post, you’ll learn how to easily control android app permissions in Delphi using the Mobile Permissions Component and Android App Builder Software. More information about the Android Permission Module can be found on DocWiki. In DocWiki, you can also learn how to configure app permissions on the project in the IDE.
With Delphi FireMonkey you can control application permissions by creating proper functions to handle permission requests, permission results, and messages to show to the user.
Here is a simple example of how you can request permission using Delphi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
procedure TFormMain.RequestPermissions; {$IFDEF ANDROID} var FPermission_SEND, FPermission_READ, FPermission_RECEIVE: string; {$ENDIF} begin {$IFDEF ANDROID} FPermission_SEND := JStringToString(TJManifest_permission.JavaClass.SEND_SMS); FPermission_READ := JStringToString(TJManifest_permission.JavaClass.READ_SMS); FPermission_RECEIVE := JStringToString(TJManifest_permission.JavaClass.RECEIVE_SMS); PermissionsService.RequestPermissions([FPermission_SEND, FPermission_READ, FPermission_RECEIVE], RequestResult, DisplayRationale) {$ENDIF} end; procedure TFormMain.RequestResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>); begin {$IFDEF ANDROID} if (AGrantResults[0] = TPermissionStatus.Granted) and (AGrantResults[1] = TPermissionStatus.Granted) and (AGrantResults[2] = TPermissionStatus.Granted) then begin end {$ENDIF} end; |
But there is another approach to work with Android permissions. Mobile Permissions Component for Android helps you to write less code and manage easily. This component has the main purpose, to facilitate the request of resource permissions on Android. With just a few lines of code, it is possible to facilitate this work in mobile Android applications.
1 2 3 4 5 6 7 8 9 10 11 12 |
procedure TForm1.OnCreate(Sender: TObject); begin MobilePermissions1.[CATEGORY].[Permission] := True; MobilePermissions1.Apply; end; procedure TForm1.Button1Click(Sender: TObject); begin MobilePermissions1.Dangerous.CAMERA := True; MobilePermissions1.Standard.AccessNetworkState := True; MobilePermissions1.Apply; end; |
Head over and check out the Mobile Permissions Component for Android on the GetIt portal and download the component within the IDE.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
MobilePermissions would be a great help.
Is it planed to work in Delphi 11?
Link above to Mobile Permissions Component for Android is no more correct, because it pointers to 0.3 version. Actual is 0.5 version.
Thanks for the heads-up Matthias. I have updated the links to point to https://getitnow.embarcadero.com/mobile-permissions-component-for-android/ which should work for all current and future versions. 👍
Btw, the first example does not compile as the 3 F… variables are not defined.
So after some addition of those 3 string variables, the result of the compilation is now
[DCC Error] frm_Permissions.pas(64): E2250 There is no overloaded version of ‘RequestPermissions’ that can be called with these arguments
Hi, there have been some significant changes around the subject of mobile permissions and Android libraries between the more recent versions of Delphi.
You can refer to the following article by MVP Dave Nottage for details on how to resolve the problem you describe: https://github.com/DelphiWorlds/HowTo/tree/main/Solutions/AndroidLibraries.
Thanks for your other comments regarding a typo – that’s been corrected now, sorry it sneaked in! 🙂