How can I increment a variable at Midnight

Hi Steve,

I have another question on this same project. I want to increment a variable by 1 at the end of each day. In other words, when it is 12 am, I want to increment the variable by 1. Is there any way I could do that in MIT App Inventor? Should I use the clock feature?

Thank you!

Could I do something like this?

You could but you would not reach your ultimate goal. App Inventor apps cannot run as a Service (in the background) using native Blocks. When your app goes to sleep or your device is turned off, your code will not execute at 00:00. Your then statement will ONLY execute at exactly 00:00 if your app is awake and the device turned on. If the app is asleep or not turned on at midnight, the algorithm will have to wait 24 hours to try again and probably fail.

What you could do is compare dates. Save a date in a TinyDB, then compare it with the present date and figure out whether it is the next day. Certainly this is not changing at midnight but will execute the next day. see Clock .

Thanks Steve for letting me. I'll take a look at it in a bit. Right now, it seems as if I'm receiving a more serious problem. When I run my app on an Android device, my app just doesn't finish running. In other words, it is just stuck somewhere in the middle of the code (I have confirmed this by putting in words in a text box and seeing if they print out). Could this be because there is a lot of people using the App Inventor website right now? Should I show you my code to see if there is any problem with that? Please let me know.

Hello Jay

No, the server is always busy and that is not a problem. Your App has hung on something within the code, or has done something to exceed the memory space it has been allocated. Observe what is happening when you run the App - when it stops, you should have a reasonable idea of what it was tasked with at that time and what part of the code would be running.

See my website for a list of things that might cause an App to freeze:
https://www.professorcad.co.uk/appinventortips#TipsApk

1 Like

Yeah, I'll try those methods out which are listed on your website. For some reason, when I tested my app yesterday morning it wasn't running through all the code but when I tested it in the night, it was working correctly. Could there be a reason for that?

Well, your broadband width may have diminished for a spell and that could account for it, a rare thing though.

The same thing happened to me this morning. I tried out the code and it worked initially but it is no longer working anymore. I'm not sure if this is just another coincidence or if it is something else.

I think a logcat session might reveal what is happening:

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Hi Taifun,

Here is a screenshot of a part of my project where I suspect the problem is. The logic is a bit complicated. Also, please keep in mind that all the screenshots are under one event and are all connected together. I just couldn't make them all fit in one screenshot.

Thank you in advance. I have initialized all these variables to their appropriate number/list/empty text box/etc.

Either right click on the block (File1.GotText) and download as png - this will create a draggable block
Or right click on empty space in blocks editor and download blocks as image (this will download all blocks then you can crop as needed)

Hi Jay

We can see from your Blocks (just about) that they could probably be more efficient. Impossible to follow the code because you have many variables but we cannot see what type they are initialised as, but 'name 71'? 71 variables to hold names?

Three items of interest:

  1. How many Screens does your App have?
  2. Does your App use images?
  3. Post (text file) the results of your Logcat session.

Hi Jay

Some good news for you is that with the soon to be released next version of App Inventor, nb184, plus a clever newly released extension by Atom Developer, you should be able to run your timer in the background as an Android Service!

Hi Chris,

It seems as if I have solved the problem in my code. Thank you for your help. I will surely read the 'Background Tasks Extension' article so that I can incorporate this in my future projects.