Upload
terence-ray
View
213
Download
0
Embed Size (px)
Citation preview
No Texting While Driving
Introduction
You'll design the app so that it sends a response to any text message received. You'll also allow the user to customize the response sent.This Lessson introduces the following App Inventor concepts:
• The Texting component for sending texts and processing received texts.
• The TinyDB database component for saving the customized message even after the app is closed.
Set up the Components
Component Type Palette Group What you'll name it Purpose of Component
Label Basic PromptLabel Let the user know how the app works
TextBox Basic MessageTextboxUser will enter custom response here.
Button Basic SubmitResponseButton User clicks this to submit response
Texting Social Texting1 The component that processes the texts
TinyDB Basic TinyDB1The component that will store the response in the database
Set the properties of the components in the following way:• Set the Text of PromptLabel to "The text below will be sent in response to
all texts while this app is running."• Set the Text of MessageTextbox to "I'm driving right now, I'll contact you
shortly".• Set the Text of SubmitResponseButton to "Modify Response" .
Add behaviors to the components
• When a text is received, the message entered in MessageTextbox is sent as a text message response to the sender.
• When the user modifies the custom message in MessageTextbox and clicks the SubmitResponseButton , the new message is saved persisently in the phone's database.
• When the app begins, the custom message is loaded from the database into MessageTextbox .
Responding to a text
• When a text message is received by the phone, the Texting.MessageReceived event is triggered.
• Your app should handle this event by setting the PhoneNumberand Message properties of the Texting component appropriately and sending the response text.
U will need the following blocks:
Block Type Drawer Purpose
Texting1.MessageReceived Texting1 the event-handler that is triggered when phone received a text
set Texting1.PhoneNumber to Texting1 set the PhoneNumber property before sending
value number My Definitions this is the phone number of the person who sent the text
set Texting1.Message to Texting1 set the Message property before sending
MessageText.Text MessageText This is the message the user has entered.
Texting1.SendMessage Texting send the message
The blocks will look like this:
Storing the custom response
TinyDB provides two blocks:• StoreValue and GetValue . • The former allows you to store a tagged piece
of information, while the latter let's you retrieve one.
You'll need the following blocks to store the custom message:
Block Type Drawer Purpose
SubmitResponseButton.Click SubmitResponseButtonuser clicks this button to submit new response message
TinyDB1.StoreValue TinyDB1 store the custom message in the phone's database
text ("responseMessage") Text use this as the tag for the data
MessageTextbox.Text MessageTextbox the response message entered by the user is here
Retrieving the saved response message Block Type Drawer Purpose
def variable ("response") Definitions A temporary variable to hold the retrieved data
text (blank) Text Initial value for the variable can be anything
Screen1.Initialize Screen1 This is triggered when app begins
set response to My Definitions you'll set the variable to the value retrieved from db
TinyDB1.GetValue TinyDB1 get the stored response text from the database
text ("responseMessage") Text plug into tag slot of GetValue , make sure text is same as was used in StoreValue above
if test Control to ask if the retrieved value has some text
> block Math check if length of retrieved value is greater than (>) 0
length text Text check if length of retrieved value is greater than 0
global response My Definitions this variable holds the value retrieved from GetValue
number (0) Math to compare with length of response
set MessageTextbox.Text to MessageTextbox if we retrieved something, place it in MessageTextbox
global response My Definitions this variable holds the value retrieved from GetValue
Blocks look:
No Text While Driving, Final Program