Writing the Code

0
195

For this exercise, we will draw three clickable sprites: one to make a phone call, one to send a text message, and one to send an email. Figure 2-2 shows our application and three native applications launched upon interactivity.
our application with three buttons

An AIR application cannot call, send a text message, or send an email directly, but it can invoke the native applications dedicated to these tasks and pass arguments such as the phone number or email address.

The URI scheme is a way to include small data items inline. AIR passes the argument to the Android system according to the official tel, sms, and email URI schemes. If the argument contains invalid characters or spaces, the command will be ignored. Valid characters are digits, dots, and the plus sign (+). Android currently does not support multiple numbers or a body argument for a text message, but it does support multiple emails and a body argument for an email.

If more than one application has a custom URI, the choices are represented in a menu. This is the case with mailto, as demonstrated in Figure 2-2, with both native email and Gmail applications.

Note that a mouse event works on a multitouch device as though it were a single-touch device.

Using Flash Professional

If you are using Flash Professional, add the following code to the Document class named Main, created earlier:

package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
public class Main extends Sprite {
public function Main() {
// create the three sprites and their listeners
var callButton:Sprite = createSprite(0xFF3300, 140, 150);
callButton.addEventListener(MouseEvent.CLICK, callMe);
addChild(callButton);
var textButton:Sprite = createSprite(0x0099FF, 140, 350);
textButton.addEventListener(MouseEvent.CLICK, textMe);
addChild(textButton);
var mailButton:Sprite = createSprite(0x00FF11, 140, 550);
mailButton.addEventListener(MouseEvent.CLICK, mailMe);
addChild(mailButton);
}
function createSprite(hue:int, xPos:int, yPos:int):Sprite {
var temp:Sprite = new Sprite();
temp.graphics.beginFill(hue, 1);
temp.graphics.drawRect(0, 0, 200, 100);
temp.graphics.endFill();
temp.x = xPos;
temp.y = yPos;
return temp;
}
function callMe(event:MouseEvent):void {
trace(“calling”);
navigateToURL(new URLRequest(‘tel:18005551212’));
}
function textMe(event:MouseEvent):void {
trace(“texting”);
navigateToURL(new URLRequest(‘sms:18005551212’));
}
function mailMe(event:MouseEvent):void {
trace(“emailing”);
navigateToURL(new URLRequest
(‘mailto:[email protected]?subject=Hello&body=World’));
}
}
}

Select Control→Test Movie→Test to compile the application. You should always run your code on the desktop before installing it on a device in case you have syntax errors.

Using Flash Builder

The code is the same.

Select the small black arrow to the right of the Run button, and then select Run Configurations. Under Mobile Application, select Main if it is not selected. The “Target platform” should be Google Android. Under “Launch method”, select “On desktop” and choose your device from the pull-down menu. Click Run to compile and create the mobile application. If you don’t see your device listed, you can add it manually.

For Windows users, if you see the name of your device in the list, it indicates that the necessary drivers are preinstalled. Otherwise, make sure to install them.