Open Source β€’ Background Tasks: Itoo πŸš€

Thanks for taking time to answer.

:frowning: as you knew, I had other problems ... I have now happily renamed my Timer procedure to TimerEventProcessing :slight_smile:

Thank-you for the explanation. I was trying to reuse some TinyDB code segments. I shall switch to itoo's FetchProperty/StoreProperty DB facility.

-Randal

1 Like

... if I may ask one more related question:

Is a global variable that is written by a foreground app available to the background process? And vice-versa: is a global variable written from the background available to the foreground? Subject to synchronization? Where could I read things like this about foreground vs. background processes and their interaction / communication?

-Kind regards,
Randal

No, use the StorePropeety/FetchProperty blocks
Taifun

1 Like

Ok ... I really did not think I was asking the same question! But I guess I did :frowning:

... thanks for your patience.

Kind regards,
Randal

It's okay to ask questions.

Well, that's good because I have plenty :slight_smile:

As I read this and other threads, I have become quite confused about background vs foreground processes/tasks/threads.

In your example, "Notification Every Second":

I assume there is a process, "P0", which executes the Button1.Click event, correct? Is it fg or bg? When does it terminate? It would seem to continue, with screen1 being "active" and visible.

Does the Button1.Click event create (via itto1.CreateProcess) a background or foreground process, call it "P1", which then executes procedure "clock"?

Does "P1" run to completion after registering the Clock1.Timer event?

What/which process executes the "timer" procedure when the Clock1.Timer event fires?

I think I will stop there for now.

I should add that this is not just of academic interest. I am aware that some things cannot be done from a background process (or an inactive process - what's the difference. Oops, there's another question:-) . For example, I find that a Notify1.ShowAlert will work from places where a toast ( [Free Open Source] my toast extension 3.0 ) does not - or perhaps there are other problems with my tests that lead me to conclude this.

Kind regards,
Randal

This is not a process it is the main activity of the app, see also Introduction to Activities in Android - GeeksforGeeks

The answer is here in post 46

See also again the definitions in post 1

Itoo supports two types of services.

  • Foreground
    • keeps the service alive throughout the session without interruptions.
    • a permanent notification will be displayed throughout the session from Oreo.
  • Background Task
    • unlike the foreground task, it is executed on a different process.
    • vulnerable to unexpected / sudden shutdown by the system (especially Samsung, Xiaomi devices).
    • the running task should be completed within 10 or 30 minutes depending on the Phone brand.
    • it is possible to restart the service from then, by setting the restart property to true

Taifun

1 Like

After registering nothing is running

Itoo will take care of it
Don't worry too much about how it is working... it is a piece of magic by @Kumaraswamy :wink:

Taifun

1 Like

Also regarding the Notifier1.ShowAlert, they are UI interactions that do not always work on across all devices, so I wouldnt recommend touching them in Itoo.

Well, clearly, this is not something I am going to understand ... so I may as well "believe in magic":slight_smile:

So now, I must let you see my app and get some help.

I want to alert the user with something like a toast or Notifier1.ShowAlert when cell data has been left on and/or is actually being used (under some circumstances, configurable by the user).

The toast I am using does not work from the background timer event processing (I figured there is a reason for this that has to do with bg/fg), but Notifier1.ShowAlert seems to be working, so I was counting on using it. Then I am advised:

So, am I going to have to give up on displaying a brief message on top of the active app and just use a notification to alert the user?

-Randal

So far I have only tested this on android 8.0, but I would like it to work up through 14.

DataMinder_toastCase.aia (902.8 KB)

Hi, if the Notifier1.ShowAlert works for you, then you may use it, but it may be inconsistent across devices. You may alternatively show a notification to the user instead:

Thanks for the clarification. I will plan to use both ShowAlert and a notification.

I have already imported the modified Notification Style (see in my blocks the calls to Has/Ask permission blocks)

Thanks again for the interaction/help.

Kind regards,
Randal

[20240125 edit]
I tried the DataMinder_toastCase test app on a Pixel running Android 12 and the toast worked! but the Notifier1.ShowAlert did not :slight_smile: So, I think I will start out with all three mechanisms, emphasize the NotificationStyle notification, but also include the ShowAlert and toast. I can allow the user to select the methods which work on his device and disable the others.

1 Like

I have just tracked down one of my programming errors which turned out to be the use of a built in Color component block where a hexadecimal number for a color was expected in a block that was being executed by an the event registered by:

events

This error of mine became obvious when I executed the same (or similar) block from the foreground portion of the app - it was clearly reported by the system when it occurred.

My question is: Is it possible to gain insight into such errors when executed in the background?

Could the Screen1.ErrorOccurred event be made to fire? Possibly by registering it with itoo1.RegisterEvent?

How about the error log? Could anything be written to the error log and viewed by "adb.exe logcat"?

Is there some way to connect (AI Companion, Emulator, USB) and debug such an app? My app foreground process closes immediately after it creates the background task. If I kept it running would background errors such as this be reported to the UI?

Any suggestions besides "don't write buggy code"? :slight_smile:

Kind regards,
-Randal

I'll possibly work on a better feature to report errors, (after two months, my exams get over).
The only way to debug right now is to check the logcat.


Itoo does not support companion debugging, even if it is someway implemented to mimic the functionality, we cannot afford its code maintainability.


So there is some kind of error happening... Did you try to use ADB to check for the error logcat? If you are able to capture it through ADB, you can send it personally to me. Most of the times errors happen when components are not initialzed or set properly...

Thank you for addressing my general and specific question.

That would seem to be a very useful enhancement. After a while, since users could see their own errors displayed, you might recover your time spent developing the capability because you would get fewer questions about problems we might be having :slight_smile:

Yes, I did. But I never was able to locate anything in the log. If you think it should have been there, then I can re-run the buggy version and try again.

Do you have any suggestions as to "adb.exe logcat" options that might eliminate entries that would not be of interest? perhaps piping to "grep XXXX".

As I mentioned, I found the error after I ran the same type buggy blocks (I had used a color when a hex number for a color was required.) in a foreground portion of the app - the error was reported to the phone screen.

I will see if I can build a small test case that exemplifies the error and then see if I can locate the error using logcat. This would be good practice for locating future bugs.

Kind regards and good luck on exams, :student: :+1:

Randal

1 Like

therefore the general advice is to first get it running in the foreground...

did you check logcat? you might find something using the tag "ItooCreator"...

you should find something using logcat...

Taifun

1 Like

Great extension with it any application made with app inventor can work in the background.

I see that there is doubt about how the extension works, I am attaching two aia files, which can help you understand how it works.

One is a step counting app that works in the background, so it will always count your steps.

Another is an Alarm manager

Both work correctly, although as usually happens, the battery mode must be set to "No restrictions" for them to work correctly on lame Xiaomi or Samsung phones.

two requests to the developer

First, the notification can be interacted within the service to change the title and subtitle and when you click on it, the activity opens with the NEW_TASK flag.

Another option is to create a kind of static procedure in the main activity that can be used in the service, so that the number of blocks can be removed from it.

AlarmaItoo.aia (164.3 KB)

ServicioIto.aia (126.3 KB)

1 Like

WOW I LOVE IT!!! That's incredible what you have done with Itoo, especially the Pedometer one, can you please publish them as a guide (a new topic) so that it could help others?


I dont get the second request, what does it mean?

Indeed I did!

Here are the blocks. The offending one its the passing of a built-in Color where a hexidecimal color number is expected (color Red)

logcat, soon after "##################", contained the following:

01-27 23:15:24.522 30979 30979 W System.err: java.lang.IllegalArgumentException: Unknown color

The logcat context is included below. (BackGndErr_dies-logcat.txt)

As I get more familiar with the format of these errors, I should be able to "grep" them out of logcat output.... Looks like a good first cut would be:

adb.exe logcat | grep "System.err"

I am a little surprised that it is only a "W" and not an "E" category, so that tells me to examine all entries, not just those of the Error category.

Thanks for your help!

Kind regards,
Randal

BackGndErr_dies-logcat.txt (3.9 KB)

Can you tell us where does the block "IbpAllAlarms.HexToColor" come from? Also you are passing a color to a block when it expects hexadecimal value (something like #845EC2)... So the error is obvious...

1 Like