Receiving Messages
If you want to build a service which is controlled by SMS messages,your program must be able to react to incoming messages. In theory, you
could accomplish this task by calling the sms messages() function at
constant intervals and checking whether the list has been changed since
the last call.
This approach is likely to perform lots of unnecessary work. Luckily,
PyS60 provides an alternative: a callback function can be called whenever
a new message is received.
Example 17: SMS receiver
import inbox, appuifw, e32
def message_received(msg_id):
box = inbox.Inbox()
appuifw.note(u"New message: %s" % box.content(msg_id))
app_lock.signal()
box = inbox.Inbox()
box.bind(message_received)
print "Waiting for new SMS messages.."
app_lock = e32.Ao_lock()
app_lock.wait()
print "Message handled!"
The Inbox object’s bind() function binds a callback function to
an event that is generated by an incoming message. In this example,
the function message received() is called when a new message
arrives.
The callback function takes one parameter, namely the ID of the
incoming message, msg id. Based on this ID, the message contents,
or any other attribute, can be retrieved from the SMS inbox, as we saw
above.
The program should not execute from start to finish at once. Instead, it
should wait for an event to occur. This time the event is not generated by
the user directly, but by an incoming SMS message. Waiting is handled
in the familiar manner, using the Ao lock object.
When a new message arrives, the function message received() is
called. It opens a popup note that shows the contents of the newly arrived
message. Then the Ao lock is released and the program finishes with
the message ‘Message handled!’. The only way to terminate this program
properly is to send an SMS message to your phone.
No comments:
Post a Comment