Talking SMS app tutorial

Tutorials with advanced 'difficulty' and more Lines of Code.

Talking SMS app tutorial

Postby jayaram » Tue Dec 15, 2009 2:34 pm

In this post, i'm describing a Talking SMS app that reads out the incoming sms messages. The app makes use of the TTS API that comes along with Android 1.6 or higher.
For getting this code to work, you need Android 1.6 platform or higher.
As this is my first article, correct my mistakes. sorry, if too much has been explained.
In our application, apart from the main class (speaker) that extends the Activity class, we create another class called SMSReceiver that extends the class BroadcastReceiver class of android.content package.

Our speaker class creates an instance for the TextToSpeech class present in the android.speech.tts package and defines a static method speakSMS(String) that is called by the SMSReceiver class when an SMS is received. The speakSMS(String) speaks out the received SMS and synthesizes the sound to a .wav file and saves it in the Sdcard, that can be retrieved later.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package mypack.mydemos.demo;
  3. import android.speech.tts.*;
  4. import;
  5. import android.os.Bundle;
  7. public class speaker extends Activity {
  8.     /** Called when the activity is first created. */
  9.         private static TextToSpeech myTts;
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState)
  13.     {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.         myTts = new TextToSpeech(this, ttsInitListener);
  17.     }
  18.     private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() {
  19.         public void onInit(int version) {
  20.           //myTts.speak(""+o, 0, null);
  21.         }
  22.       };
  24.    public static void speakSMS(String sms)
  25.    {
  26.            myTts.speak(sms, 0, null);
  27.            myTts.synthesizeToFile(sms,null, "/sdcard/myappsounds/mysms.wav");
  28.    }
  29. }
Parsed in 0.014 seconds, using GeSHi

Create a class called SMSReceiver that extends BroadcastReceiver class. Then, you have to override the method onReceive().
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package mypack.mydemos.demo;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.telephony.SmsMessage;
  8. import android.widget.Toast;
  10. public class smsreceiver extends BroadcastReceiver {
  13.         @Override
  14.         public void onReceive(Context context, Intent intent) {
  15.                 // TODO Auto-generated method stub
  16.                                 int n;
  17.                                 Bundle bundle = intent.getExtras();
  18.                                 Object pdus[] = (Object[]) bundle.get("pdus");
  19.                                 SmsMessage smsMessage[] = new SmsMessage[pdus.length];
  20.                                 for (n = 0; n<pdus.length; n++)
  21.                                 {
  22.                                 smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
  23.                                 }
  24.                                 // show first message
  25.                                 String sms1 = smsMessage[0].getMessageBody();
  26.                                 String from = smsMessage[0].getOriginatingAddress();
  27.                                 Toast toast = Toast.makeText(context,"SMS Received from "+from+":" + sms1, Toast.LENGTH_LONG);
  28.                       ;
  29.                                 speaker.speakSMS(sms1);                        
  30.                         }
  32.         }
Parsed in 0.011 seconds, using GeSHi

Then we have to edit the AndroidManifest.xml file for two reasons. One is to register the BroadcastReceiver that we are using in our application. We can do this by using a <receiver> tag inside the <application> tag.
The other is to obtain the permission for receiving Incoming SMS messages in our application. We can do this by using a
<uses-permission> tag inside the <manifest> tag.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.      package="mypack.mydemos.demo"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".speaker"
  8.                  android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>    
  14.       <receiver android:name=".SMSReceiver" android:enabled="true">
  15.                         <intent-filter>
  16.                                 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
  17.                         </intent-filter>
  18.                 </receiver>
  19.     </application>
  20.     <uses-sdk android:minSdkVersion="5" />
  21.     <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
  23. </manifest>
Parsed in 0.002 seconds, using GeSHi

By running the application, the Text to speech object will be instantiated. For testing purpose, you can send sms to your emulator from the terminal. For that, open a terminal and type
telnet localhost 5554
and press enter. You will be logged in into the Android console. For sending a test sms from there, type
sms send <sender no.> <message>
Once you type the message and hit enter, you can immediately see the message received by the emulator as shown in the image below. Also you can hear the emulator speaking out your message.
The voice may not be clear since it is spoken out from the emulator. As we have saved the voice in the emulator's sdcard, we can pull out the sound file using the Eclipse's DDMS plugin and play it locally from your computer. Now, you will feel the voice clear.
I'm attaching the source code with the screenshots.

Thanks for reading. Pass your comments.
(175.41 KiB) Downloaded 1082 times
Junior Developer
Junior Developer
Posts: 16
Joined: Wed Jul 29, 2009 3:37 am
Location: India


Postby cmicali » Sat Feb 27, 2010 5:45 pm

Thanks for the example... is there any problem with keeping the TextToSpeech object around for the lifetime of the app? It seems fairly expensive to initialize this object, so I wasn't sure if it is OK to keep a handle to one for a long time (especially if your app is always running in the background.)
Once Poster
Once Poster
Posts: 1
Joined: Sat Feb 27, 2010 5:43 pm

Re: Talking SMS app tutorial

Postby Vandorin » Thu Sep 23, 2010 6:08 am

Great tut, am I allowed to use this code in my app? I'd credit you of course :D
Posts: 2
Joined: Sat May 22, 2010 10:46 pm

Re: Talking SMS app tutorial

Postby schizophrenic » Sat Mar 03, 2012 2:24 pm

sir is there a codes? that reads also the sender?tnx
Posts: 3
Joined: Sat Mar 03, 2012 1:42 pm

Re: Talking SMS app tutorial

Postby worried » Tue Mar 19, 2013 4:00 pm

I am new to android developmant.

I am trying to develop an app where on incoming call, the app will speak " Incoming call from SO and SO".

For this purpose I am using tts.

I am sending a string Incoming call from NAME

mytts.speak(string, 0, NULL);

But the voice generated is interrupted for unknown reasons..

I even tried TextToSpeech.QUEUE_FLUSH option

But in vain.

I have even turned the ringing volume to zero and infact there is no ringing tone!! But the voice is still being interrupted!!!!!!!
Once Poster
Once Poster
Posts: 1
Joined: Tue Mar 19, 2013 3:57 pm


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests