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.
speaker.java
Using java Syntax Highlighting
- package mypack.mydemos.demo;
- import android.speech.tts.*;
- import android.app.Activity;
- import android.os.Bundle;
- public class speaker extends Activity {
- /** Called when the activity is first created. */
- private static TextToSpeech myTts;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myTts = new TextToSpeech(this, ttsInitListener);
- }
- private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() {
- public void onInit(int version) {
- //myTts.speak(""+o, 0, null);
- }
- };
- public static void speakSMS(String sms)
- {
- myTts.speak(sms, 0, null);
- myTts.synthesizeToFile(sms,null, "/sdcard/myappsounds/mysms.wav");
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Create a class called SMSReceiver that extends BroadcastReceiver class. Then, you have to override the method onReceive().
SMSReceiver.java
Using java Syntax Highlighting
- package mypack.mydemos.demo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.telephony.SmsMessage;
- import android.widget.Toast;
- public class smsreceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- int n;
- Bundle bundle = intent.getExtras();
- Object pdus[] = (Object[]) bundle.get("pdus");
- SmsMessage smsMessage[] = new SmsMessage[pdus.length];
- for (n = 0; n<pdus.length; n++)
- {
- smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
- }
- // show first message
- String sms1 = smsMessage[0].getMessageBody();
- String from = smsMessage[0].getOriginatingAddress();
- Toast toast = Toast.makeText(context,"SMS Received from "+from+":" + sms1, Toast.LENGTH_LONG);
- toast.show();
- speaker.speakSMS(sms1);
- }
- }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
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.
AndroidManifest.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="mypack.mydemos.demo"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".speaker"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name=".SMSReceiver" android:enabled="true">
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="5" />
- <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
- </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
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.



