Monday 18 May 2015

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.

No comments:

Post a Comment