Sunday, June 5, 2011

Creating my first Android app, Part 4

This is the last post in a four part series. Go here to read Part 1, Part 2, and Part 3.


This is the final blog about creating apps. First, I'm going to clean up some functionality from the last blog. Second, I'm going to show you how to email a note.

If you "packaged" the app and downloaded to your phone, you would notice it worked, but notes would not be saved between instances of the program. Actually, the notes are saved, but the list of notes aren't.

Here's the solution. We need to store the list of note titles in the database every time we save a note, and then load them every time we try and load a note. It involves modifying two blocks. First, modify the [ButtonNoteSave.Click] block:

  1. Click [My Blocks], click [DatabaseNotes], click+drag [call DatabaseNotes.StoreValue] just below the [call add items to list] block.
  2. Click [Built-In], click [Text], click+drag [text text] to the "tag" slot. Click on the second "text" on the [text text] block, which allows you to edit it. Rename it "TagNoteTitles". This will be the tag where we will store the list of Note Titles. Confusing enough for you?
  3. Click [My Blocks], click [My Definitions], click+drag [global ListNoteTitles] to the "valueToStore" slot.
Now when you save a note, the current list of note titles will be saved into the database. That way, if we close the app and start it again, that list of titles is stored. As I said before, there is no easy way to extract what exactly is in the database, so we need to keep track of it.

Second, we need to load the list. This can be done in several different places, but for simplicity, lets do it before we load a note. We need to modify the [ListNoteLoad.BeforePicking] block.
  1. Click [My Blocks], click [My Definitions], click+drag [set global ListNoteTitles to] just above the [set ListNoteLoad.Elements to] block.
  2. Click [My Blocks], click [DatabaseNotes], click+drag [call DatabaseNotes.GetValue] to the "to" slot.
  3. Click the block we created above named [text TagNoteTitles]. We want to copy it, so press CTRL+C, and press CTRL+V. This should create a duplicate of this text block which we can drag over to the "tag" slot on the [call DatabaseNotes.GetValue] block.
So before we load a note, the list named "ListNoteTitles" is pulled from the database.

Emailing a note

On to being able to email yourself (or some other person) a note. The biggest problem I had with other note apps was the notes were trapped on the phone. To get them off, you had to cut and paste the note into an email.

The basic idea is the note title is the email subject, the note message is the email body, and the email address in the TextBoxEmail is where the email is to be sent. The app will call a component called Activity Starter (which we named AppEmail). Activity Starter can be used to start the camera, open the browser, open a map, etc.

The steps are below:
  1. Click [My Blocks], click [ButtonEmail], click+drag [when ButtonEmail.Click] to the editor.
  2. Click [My Blocks], click [AppEmail], click+drag [set AppEmail.Action] to the notch.
  3. Click [Built-In], click [Text], click+drag [text text] to the "to" slot. Edit the text to say "android.intent.action.VIEW".
I'll be honest; I'm not sure what this does exactly. But I read it on another blog and it works. Did I shatter any ideas you had that I wasn't a hack? Good. Now we have to pass all the interesting information to the Activity Starter. This involves a huge string of text with the email address, subject, and body.
  1. Click [My Blocks], click [AppEmail], click+drag [set AppEmail.DataUri] to the notch below [AppEmail.Action].
  2. Click [Built-In], click [Text], click+drag [call make text] to the "to" slot.
  3. Click [Built-In], click [Text], click+drag [join] to the "text" slot. The [call make text] block will create a new open slot named "text". Click and drag two more [join] blocks to the [call make text] block so there are three total.
  4. Click [Built-In], click [Text], click+drag [text text] to the [join] blocks. It needs to go to the first slot. Do this three times for all three [join] blocks. Edit them to say "mailto:", "?subject=", and "&body=".
  5. Click [My Blocks], click [TextBoxEmail], click+drag [TextBoxEmail.Text] to the top right [join] block. The other two need to be filled in with [TextBoxTitle.Text] and [TextBoxMessage.Text]. I think you are getting the hang of this by now.
  6. Click [My Blocks], click [AppEmail], click+drag [call AppEmail.StartActivity] to the notch below [set AppEmail.DataUri].
When the [Email Note] button is pressed in the app, your email program will come up ready to send the note. All you have to press is [Send]. (Your phone may ask you to specify which mail program to use, if you have more than one.)

So there you go! You have a useful app for recording, playing, storing, and emailing notes. You can go on to add a bunch of extras, like bitmaps for the buttons, icons for the app, etc. to make your app look really pretty (see the picture above for my finished app -- note the gaudy yellow/red/blue color scheme). If you have any questions, please feel free to post comments on my blog. They are moderated (only because I was getting a bunch of Chinese spam) but I usually respond within a day or so. And by "so", I mean weeks or months later. Or never. These are all acceptable definitions of "so'.

I may, at sometime in the future, decide to bore everyone again, and post some of the games I've made. Or maybe not.


Wednesday, June 1, 2011

What catchy title for a post uses the word thermodynamics?

Understanding thermodynamics can help you in the world (seriously). There are four laws of thermodynamics, but one and two are most interesting. They are paraphrased below:

Law 1 - Energy cannot be created or destroyed, it can only change form.
Law 2 - When changing form, some energy is always lost as waste heat.

Put another way: "Not only can you not win, you can't break even."

Practical application #1
I read a story about a plan to put wind turbines near a highway to try and recapture the wind created by the fast passing cars. Sounds like a great idea, right? Use Law 1 above and tell me what is wrong with it.

Where does the energy come from? When the highway is empty, the wind turbine won't turn, so the cars must be providing the energy. And that is exactly what they found when they tested it. The fuel efficiency of each car was found to be reduced slightly. The energy created by the cars was being changed from mechanical energy (motion of the car) into wind energy (motion of the air molecules) into mechanical energy (motion of the turbine) into electrical energy.

The worst part is, as Law 2 states, every time you change form, you waste some energy. So you are basically burning gasoline to create electricity, but in the most inefficient way possible. It is sort of a Rube Goldberg contraption of idiocy.

So you may be asking yourself, "Where exactly is the practical application of thermodynamics you promised?" And here it is. You can yell at the monitor when you read these types of dumb ideas and your partner and/or dog and/or fish and/or D&D action figure can nod their head at how smart you are. Or maybe shake their head at how crazy you are. I'm not sure.

Practical Application #2
If the 1st practical application wasn't enough, here is a second one, which actually involves money and prison!

I had a co-worker pitch an idea for an invention to me. He said that his brother-in-law had a patent for a machine that could generate energy very cheaply. It was made from an outer hub with seven magnets, which was attached to an inner ring with .... (he paused and looked around, like he was about to blow my mind and didn't want any other minds around us to blow -- no collateral mind blowing) six magnets. "Because the magnets are spaced like that, the hub magnets are always pushing the ring magnets around, like a merry-go-round that never stops."

I'm sure you've all heard of perpetual motion machines. Now contrary to popular belief, the US Patent office will not reject the filing of a patent just because it is for a perpetual motion machine. However, it does stipulate that a working model must be provided before the patent will be granted. Needless to say, that has never happened.

Here's the thing. It is possible to build a "perpetual" motion machine with magnets. The reason is magnets do have energy in them, so as the machine moves it will slowly weaken the magnets. This obeys Law 1. However, taking the final step and trying to generate electricity from it can be a tricky step. Law 2 says that every time you try and change the form of energy, you piss some of that energy away. So the very act of hooking up a generator to your spinning 13 magnet science project will cause it to either spin slower or maybe weaken the magnets faster. In any case, you are just using the magnets like batteries and they will eventually die out, requiring you to get more magnets.

You may have been waiting for the money and prison part. Here it is. My co-worker told me about this incredible invention and asked me if I wanted to invest in it. His brother-in-law was selling stakes in the company, which could be "worth millions". I explained how perpetual motion machines were impossible without an energy source. My co-worker looked at me, and then tried to explain the 6 magnet / 7 magnet configuration again. He even produced a sketch on some graph paper which "proved" it. Did I mention this co-worker had a degree in Engineering? I politely looked at his sketch and explained that you couldn't violate the first or second law of thermodynamics, which this idea clearly did. I warned him that he should not invest, and should explain thermodynamics to his brother-in-law next time they meet. "Well, I won't be seeing him for a while. He's in prison on fraud charges." I got up and slowly backed out of his office, which was a surprisingly awkward way to leave an office.

Moral of the story: There is no thermodynamic law against being a moron.

PS. I swear to dog this story is true.