Thursday, September 17, 2015

App a Day 3: Morse Code Translator

Uses Classes: Handler, Thread, Message

This was a great exercise in concurrent communication. I highly recommend anyone learning to write this app in this way. I was getting frustrated that none of my previous apps used concurrency so I thought this project was perfect for that.

To my knowledge this program was a common assignment for SJSU's mobile development course.

A great resource for handlers was this video:

First I need a Runnable class. This is Translator which extends Runnable. The constructor parameters take a string and my Handler. The Handler is going to transfer messages from the Translator thread to the UI thread. A Runnable starts at the overridden run method. This calls translate. I've omitted translate from the gist, but it loops through the letters of missive, then loops through each letter's corresponding code array of DOT's DASH's and OFF's. At each iteration of the code array (and spaces in between) call blink.

Blink is where the Handler sends messages to the UI thread. Obtain a Message and set arg1 to white or black and arg2 to the character we are currently translating. Call Handler.sendMessage and then sleep.

When sendMessage is called the Handler calls handleMessage over on the UI thread. It sets the color and displays the letter by what the message just delivered via the Handler. To start everything in motion, make a Thread object that references the Translator and then call start on that thread.

1 comment: