I have an Android application, which runs a local service in the background which needs to connect to a Datasnap server, get some data, and inform the user with a notification if some data is found.
Everything is written in Delphi 11.0, and everything works (Datasnap server works, application connects to it, service runs, notifications from service displays on regular basis) until the point when I try to launch some Datasnap server functions (they work in an application and compile without problem), or simply try to open a TSQLConnection
component from a service (located on the service's TAndroidService
form, generated by the Android Service wizard). I can open it while in the IDE, but can't from the service, and the service stops responding after such a call.
Can someone point me to why it's not working, or how to debug the service?
P.S. I discovered that the service can connect to the DataSnap server while the program is active. However, if the application is closed, the service (foreground) can perform local operations but is unable to connect to the server.
It looks like you have a problem with the behavior of your Android service application in Delphi 11.0 when the application is running in the background and wants to access your DataSnap server. Here are some steps that might help you diagnose and fix the problem: Check permissions: Make sure your service has the correct network permissions. Check your AndroidManifest.xml file for the required permissions such as INTERNET or other specific permissions necessary to access your server. Service Lifecycle Management: It is important to understand how services are managed on Android, especially when it comes to long-running operations. Check whether your service starts correctly and is not terminated by the system due to lack of memory. Debugging the service: If you want to debug the service, you can add log output and view it via ADB logcat or use a debugger attached to the running process. This can help you determine at what point the code stops responding. Be aware of background restrictions: Modern versions of Android severely limit background activities to improve battery performance. If your app goes into the background or the device is put to sleep, network operations may be interrupted. Network call management: Ensure that network calls are properly managed in the background. You may need to use WorkManager or JobScheduler to ensure that your tasks can run even when your app is not in the foreground. Datasnap configuration review: Review all settings related to the TSQLConnection object and ensure that none of them contain a condition or configuration that may not be met when the application is running in the background. Error handling: Implement appropriate error handling mechanisms in your service code to determine exactly where it is failing - e.g. timeouts during connection attempts or unexpected exceptions when calling server functions. Testing with different states: Test your app in different states: while it is active, after it has just been closed, and after a long period of non-use. Check network access in standby: If your problem occurs when the device has been idle for some time and then tries to connect (device "Doze" mode), Firebase Cloud Messages (FCM) could be used to periodically "wake up" the device.