AI2 apps cannot run in the background. There is no reliable solution to this behavior when the phone goes to sleep.
There are third party apps that claim to allow AI2 to not sleep or if it does sleep to handle certain activities.
This question has been asked numerous times: Follow the links by searching in the forum using the Search for topics box at the top of this page next to the colorful Google. Search for: app sleeping, run in the background, count down timer etc. or on the Internet, search app inventor + similar terms
There are lots of AI2 users who would like AI2 to run in the background, It does not. Sorry.
Regards,
Steve