Mobile android applications developed in C++ Builder requires permissions to use some part of mobile devices (sensors, camera etc..). These should be allowed by the user. an these permissions can be checked over the project options from Project > Options > Application > Uses Permissions section. Some of these permissions were listed in this document http://docwiki.embarcadero.com/RADStudio/Rio/en/Uses_Permissions. All Android permission can be found listed officially on Manifest Permission List
Some Basic Android Permissions are Set for All FireMonkey Android Apps
- Access coarse location
- Access fine location
- Call phone
- Camera
- Internet
- Read calendar
- Read external storage
- Read phone state
- Write calendar
- Write external storage
These checks are not enough, You also need to request permission on runtime. Because these permissions should be allowed manually by user and these might be not allowed. For example if you are starting to use camera in your application you must do this request check. To do this u need to Request all permissions you need by using TAndroidHelper::Activity->requestPermissions( ) like this;
1 2 3 4 5 |
TJavaObjectArray__1<_di_JString> *perms = new TJavaObjectArray__1<_di_JString>(1); // 1 indicates number of permissions will be requested perms->Items[0] = TJManifest_permission::JavaClass->CAMERA; TAndroidHelper::Activity->requestPermissions(perms, MY_PERMISSIONS_REQUESTCODE); |
and you must check permissions if it is granted or not by using checkSelfPermission() like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
if( (TJContextCompat::JavaClass->checkSelfPermission(TAndroidHelper::Activity, TJManifest_permission::JavaClass->CAMERA) != TJPackageManager::JavaClass->PERMISSION_GRANTED) ) { // if Permissions are not granted ask for permission here } else { // Permission has already been granted, we can start camera ActionTakePhotoFromCamera->Execute(); } |
We should show permissions so user can grants manually;
1 2 3 4 5 6 7 8 9 |
if ( TJActivityCompat::JavaClass->shouldShowRequestPermissionRationale(TAndroidHelper::Activity, TJManifest_permission::JavaClass->CAMERA) ) { } else { } |
Android official page has a good article about Request app permissions permissions in Kotlin & Java. C++ Builder uses Helpers to request these permissions as below;
Here is a sample for an Android Application to check permissions
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
void __fastcall TForm1::request_permissions(void) { TJavaObjectArray__1<_di_JString> *perms = new TJavaObjectArray__1<_di_JString>(5); perms->Items[0] = TJManifest_permission::JavaClass->ACCESS_COARSE_LOCATION; perms->Items[1] = TJManifest_permission::JavaClass->ACCESS_FINE_LOCATION; perms->Items[2] = TJManifest_permission::JavaClass->CAMERA; perms->Items[3] = TJManifest_permission::JavaClass->READ_EXTERNAL_STORAGE; perms->Items[4] = TJManifest_permission::JavaClass->WRITE_EXTERNAL_STORAGE; TAndroidHelper::Activity->requestPermissions(perms, MY_PERMISSIONS_REQUESTCODE); } void __fastcall TForm1::Button1Click(TObject *Sender) { // Using helper class from Android Support Library, so no OS version checking required if( (TJContextCompat::JavaClass->checkSelfPermission(TAndroidHelper::Activity, TJManifest_permission::JavaClass->CAMERA) != TJPackageManager::JavaClass->PERMISSION_GRANTED) && (TJContextCompat::JavaClass->checkSelfPermission(TAndroidHelper::Activity, TJManifest_permission::JavaClass->READ_EXTERNAL_STORAGE) != TJPackageManager::JavaClass->PERMISSION_GRANTED) && (TJContextCompat::JavaClass->checkSelfPermission(TAndroidHelper::Activity, TJManifest_permission::JavaClass->WRITE_EXTERNAL_STORAGE) != TJPackageManager::JavaClass->PERMISSION_GRANTED) ) { // Permissions are not granted. // Should we show an explanation? if ( TJActivityCompat::JavaClass->shouldShowRequestPermissionRationale(TAndroidHelper::Activity, TJManifest_permission::JavaClass->CAMERA) || TJActivityCompat::JavaClass->shouldShowRequestPermissionRationale(TAndroidHelper::Activity, TJManifest_permission::JavaClass->READ_EXTERNAL_STORAGE) || TJActivityCompat::JavaClass->shouldShowRequestPermissionRationale(TAndroidHelper::Activity, TJManifest_permission::JavaClass->WRITE_EXTERNAL_STORAGE) ) { // Show an explanation to the user *asynchronously* -- don't block this thread waiting for the user's response! // After the user sees the explanation, try again to request the permission. TDialogServiceAsync::ShowMessage("The device allows permissions", [this](TModalResult AKey) { request_permissions(); }); } else request_permissions(); } else { // Permission has already been granted ActionTakePhotoFromCamera->Execute(); } } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition