Monday, 18 May 2015

MIT App Inventor - Reading and Writing a textfile

Textfiles

Getting information into and out of text files doesn't sound glamorous, but it's one of the most useful basic tools you can learn from any programming language. What I have here is a simple example that allows you to create and add to a text file called 'datastore.txt' and then read out information stored in that. Why might you want to do that? Well lets say you wanted to make a calendar program that notified you of particular events and/or times using notifications on your phone. A simple way of doing this would be to have all the information about alerts stored in a text file on your phone (you could event set up a button to download it from a website!), have your app read that information every day and figure out from it when to make alerts appear on your phone.
To get started you will need a 'File' element from the 'Storage' menu (but in the example above I've also included a textfield, label and a couple of buttons.)


In the code above we do 3 things:

  1. whenButton1.click - adds the text from the textbox to the file (append means 'add to the end of')
  2. whenButton2.click - reads all the information from the file
  3. gets the information read from the file and displays it on screen as the contents of the label field

Challenge:

Make a text file (either by making one and putting it on your android device or writing and app to make one) that contains a time (in seconds) followed by a comma followed by some text, like this:
     10000, "10 seconds have passed"
Then write a program to read this information, use the number to control the TimerInterval of a Timer element and the text to fill in the content of a notification box that appears after the TimerInterval has elapsed. (More info on the Notifier and Timer elements here). Good Luck!

MIT App Inventor - Notifications, Texting and Timer

Notifications

App inventor notifications are a simple way of displaying alerts for particular events on your phone. First off you will need a notifier element (from the user interface menu in designer). Then you will need to decide what is going to trigger the display if the alert. It could be receiving a text, being at a specific location, or (as in the greyed out code block on the right in the above image) it could be triggered by a change in the orientation of the phone. The Notifier element has a number of methods, but two particularly useful ones are ShowAlert and ShowMessageDialog, both of which have examples of use shown above. One is triggered by a timer element (explained in more detail below) and one by an orientation sensor. all the inputs to these methods allow you to control how the notification appears and exactly what it says. Have a crack at getting the OrientationSensor example working (you can find the Orientation sensor element under 'Sensors' in the Designer panel).

Texting

Texting from these apps is remarkably simple (fair warning though, you still incur text charges when you use this app to send a text as you would usually!). The Texting element can be found under 'Social' menu if the Designer panel. The example code in the image above shows how to make a button send a text by using the SendMessage method. Simple right? Well yes, however you need to set the message destination number and content before SendMessage will actually do anything. You can either do this manually in the Designer panel, or you can use other methods on the Texting element to set the message body and recipient number.

Timer

Most of the content above is concerned with setting up and managing the Timer element. Timer is part of the 'Clock' element under the 'Sensors' menu. In the image above the top left and right blocks to two things; allows you to set the time for a notification to occur (using a timepicker element) and then sets a default value for the time picker. So what exactly is going on?
The timer element works like and alarm clock, you give it a certain amount of time (usually in seconds) and, once the timer is Enabled and that period of time has elapsed, the timer element with generate a timer event. You can use this timed event to trigger other events using the 'whenClock1.Timer' element (as I do for displaying a notification above). You set the length of this time interval using 'TimeInterval', and it expects a value in seconds.

WARNING: The timer is super useful but can also get out of control easily. If you set your timer interval too low, then events (like notifications) will trigger multiple times each second and if you have too many your application will crash (as happened to me numerous times). My suggestion whilst you're learning about timer is to have the event you have triggered by it set TimerEnabled to False after it has completed (as I do above) to prevent the Timer triggering too many times!

Challenge: 

Make an app that notifies you 5 seconds after the orientation of your phone has changed.