I tested the accelerometer in foreground and it works.
I am using Android 14.
What exactly do you expect the example to do?
It does not much, see again the documentation
The above example demonstrates how properties Sensitivity and Minimum Interval for the Accelerometer Component is saved and reapplied in the background.
You probably like to do something else? What exactly?
Taifun
I have a question.
I am converting my app to service running in background by Itoo extension.
My app includes two Clock
components that I have already rearranged to trigger in background by RegisterEvent
. Now I have this event too, that is triggered continuously by one of background Clocks:
Unfortunately my app does not work, then I figured the reason out: I forgot to transform this Event
block also to make it trigger in background, as per Kumaraswamy's guidelines. But such Event
includes a parameter (text read from file): how I would be supposed to convert it to background event? All examples I have seen do not include any parameter in the Events used.
Thanks in advance for any help.
the event handler you have to create must have the same number of arguments, in this case the argument text
Taifun
I really need for help.
After applying last Taifun's suggestion (thank you again, Taifun), I just finished converting my app to background service. Unfortunately, my app is not fully working. Long story short, my app continuously checks and fetches a file generated by another app and containing the working time marks (ins and outs), in order to calculate my working time spent and show it in a notification message. Here below the blocks of my app:
As core of my app, there are 3 events triggering in background (highlighted in yellow):
- one main Clock Timer, that sets the rate of check and read of time marks file
- the read file Event, in charge of times calculation (including a check when it is time to stop the background service)
- a secondary Clock Timer, for a delayed stop of the background process.
Since the size of my app is quite big, I highlighted (in red) also the sections to focus and concerning my problem. Background process stopping action can occur in 3 ways:
- by pressing the button in the interface of the app
- by pressing the button in the notification message
- programmatically, when - based on times calculation - my app decides that the moment has come to stop the background service
The malfunction is related to the buttons (in the app interface and in the notification message): when pressed, the service does not stop, but it keeps running. On the other hand, programmatical stop of the background service works like a charm.
I post here a video of my app showing the problem (the app visible at the beginning of the videoclip is that one producing the working time marks file):
Any help will be very welcome and appreciated. Thanks in advance.
Unfortunately your latest screenshot is of bad quality, so I can't check the blocks...
Did you get that running in the foreground?
Your previous screenshot shows us, that you are trying to use the file component, however the file component is not able to read a text file which is owned by another app... see also Some basics on Android storage system
you will need SAF or in case you do not plan to publish your app in Google Play you can ask for manage external storage permission
Taifun
Hello Taifun, thank you.
I uploaded the high quality screenshot of my app blocks on WeTransfer:
I confirm the app has been made successfully working beforehand in foregrond, then I converted it to background service by Itoo extension, as per Kumaraswamy's guidelines.
As component for file access I used MFile extension (MFile - a modified version of Sunny's `EFile` extension - Shared folders - storage permissions - Android 11+) and I confirm it works fine with the background service. The problem is related only to service stopping by buttons.
Regards.
Which Android device and Android version are you using for your tests?
EDIT: I just found what you said earlier
Let me suggest you to test this again... create a test project as simple as possible... modify the text file manually and then check, if your app (apk file) is able to read the modified text file... for newer Android versions it should not be able to read it anymore, because as already said
Taifun
instead of trying to stop running clocks in the background on button click, just stop the background service and use the StopProcess method.
Alternatively you might want to experiment with these methods
Or use again the StoreProperty/FetchProperty methods to stop a clock in the background...
Taifun
Hello Taifun, thank you again for your feedback.
To be honest, my first trials were on my old phone, that runs an older Android version. So, according to your suggestions, I tried again my app on my new phone, that is Android 15 based and that will be the actual device where my app is supposed to eventually run. My app behaviour is the same of the videoclip I posted yesterday, and I have no problem in reading the file, even by background service, since it is created in a directory inside the external storage (file:///storage/emulated/0/my-working-time/provisional-today-times.txt
).
Let's instead focus on the actual problem: from my previous picture, I picked up only the most remarkable blocks, that I suspect are concerned in the malfunction:
I already use StopProcess
(see picture above) and I don't want modify and overcomplicate an app that is supposed to make a simple operation (by the way, you were previously suggesting me that Broadcast
feature was not necessary for my case).
So, my only and actual question is: why I can successfully stop the service in BackgroundMainTimer
Event (see red comment "Background service stopped here OK"), but I can't when I push the buttons (see red comments "Background service stopped here does not work")? The operations performed are exactly the same: so, why the effect is not the same?
The only different condition I see is the value of AppGoingToClose?
flag (it is set at the end of BackgroundPrvFileTimesGotText
Event, it is visible in the bottom of my above picture), that decides if the moment has come to stop the service: when I press the buttons, that flag is still False, but under the logical point of view this should not affect the app behaviour...
I post here aix file of my project:
WorkTimeNotifyService.aia (304.2 KB)
So if you want to stop the background processing, why not only use StopProcess? Trying to additionally stop the clocks is not necessary imho...
After you modified the file outside of your app? I doubt it...
Taifun
Hello Taifun,
I followed your last suggestion, but I see a severe problem concerning the handling of Clock component in background and I can't understand the reason.
I simplified as more as possible my app, by stripping away all the unneeded features, and I included a notifier to show an alert message in the spots when the secondary Clock (BackgroundStopTimer
) triggers and when StopProcess
procedure is executed.
Then, as you advised, I set to stop straightly the service inside the buttons events, without passing by the secondary Timer.
Apparently, MainClock.TimerEnabled
and StopClock.TimerEnabled
instructions are effective only inside BackgroundMainTimer
event; whenever invoked by buttons or elsewhere, they are totally ignored, so main timer Clock keeps running.
Here the blocks of my debug trial:
And here a videoclip showing the malfunction:
Here below the AIX project file of this debug version:
WorkTimeNotifyService_DEBUG.aia (278.4 KB)
I badly need for help in order to exit such empasse.
Thanks in advance.
Please explain what exactly is not working
What do you expect and what happens instead
Taifun
In the debug version I included the button Set All Times to set the flag AppGoingToClose?, that decides when background service and timers have to stop.
The expected behaviour is that one related to Set All Times button: the timers are properly handled and eventually the clocks and background process are correctly stopped.
On the other hand, when I use the buttons (Stop Work Notify Service in the app interface or Stop Service in the notification), background process and timers do not stop, even though they are expressedly disabled/stopped by StopService procedure. In both case, StopService procedure is invoked (see the alert messages), so why in the latter case process and timers do not shut off?
Do not use the Notifier.ShowAlert method in the background... in the background there is no user interface... for debugging use the Notifier.LogInfo method instead and check logcat
Taifun
Hello Taifun, thank you again for your prompt feedback.
I would to simplify the things even more and I decided to resume my basic example app, that I used as template for the development of my actual app. Here below its blocks and AIA file project:
SimpleItooProject.aia (155.4 KB)
At that time, I tested such app on my old phone, that runs an older Android version (10 or 11) and it was working like a charm. Today, I tested it also on my new phone (Android 15 based) and I see the same exact problem: the disabling operation of the Clock (Clock1.TimerEnabled = False
) and/or Itoo.StopProcess
operation is ineffective, so the timer keeps triggering in background.
Therefore, it is quite strange that there is a case where my actual app stops background service and timers successfully, since it is supposed to not.
At this point, I am going to suspect that Itoo extension has some inconvenience with the latest Android versions, so it would be nice to get the opinion of Kumaraswamy as well.
Please check logcat for any errors
Taifun
Hello Taifun,
As per your suggestion, both on my old phone and new phone, I generated logcat log file of only Error level messages (adb logcat *:E
) with Simple Itoo Project app running:
logcat-simple-itoo-project-old-phone.txt (13.1 KB)
logcat-simple-itoo-project-new-phone.txt (77.7 KB)
While logcatting, the app has been launched, then I started background process, then I closed the app, finally I tried to stop the process by the button on the notification message. On the old phone, process/timer has stopped successfully. On the new phone, as I already told you, process/timer does not stop and the timer keep triggering, so I tried up to 5 times to stop it, but in vain.
I had a look at the logs and I see errors occurring both on old and new phone, but honestly I have no clue how to read and decode these logs.
As usual, any help from your side will be very appreciated.
Hi yes, Stop Process seems to be ineffective on latest version of Android. This needs to be fixed.
Hello, can you help me and tell me why it doesn't work if I want to use the web component in the background?
look these are my blocks and it doesn't work in the background
I tried this way and it works but if I want to use the web component it doesn't work
Could you tell me what I'm doing wrong? Thank you I await your response