Wednesday, March 16, 2011

Let Android read my SMS

Another I tried on SL4A is to play with their SMS function. So, with the resulting code below:

  1. As usual import library, and create the Android() object
  2. and from the Android object call smsGetMessages, with a required parameter for unread message, True for unread only, false for other wise.
  3. and call the build in android Text To Speech software to read it out, by calling ttsSpeak method.



import android
droid = android.Android()
result = droid.smsGetMessages(True)
for i in result.result:
droid.ttsSpeak(i['body'])

the result in the output is a list of such dictionary, in python notnion. Since I only want the message so I call it by i['body']

{u'_id': u'59',
u'address': u'Address of sender aka the phone no',
u'body': u'Message Body',
u'date': u'1300254988000',
u'read': u'1'}
the date is the datetime read and the read is 1 is read, and 0 otherwise. The ttsSpeak method is easy to use too, just pass in a string.

Originally I read all messages, and pass to the tts library. Turn out to be a bad idea, because I have no idea how to stop it from speaking once it started....

No comments:

Post a Comment