Following are the classes that i’m trying to implement, but i dont know where should i speak the name that i got from brodcast receiver.Can anyoneone help.
SERVICE CLASS
public class SMSTalk extends Service implements OnInitListener, OnUtteranceCompletedListener {
public static TextToSpeech mTts;
private String spokenText;
public String msg=null;
int flag=0;
@Override
public void onCreate() {
mTts = new TextToSpeech(this, this);
// This is a good place to set spokenText
}
public void readName(String temp)
{
msg=temp;
System.out.println("HHHHHHHHHHHHHHHHHHH"+msg);
// mTts.speak(msg, 0, null);
}
@Override
public void onInit(int status) {
SMSReceiver smsReceiver=new SMSReceiver();
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.UK);
if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
System.out.println("@@@@"+msg);
Toast.makeText(getApplicationContext(), "SUCCESS",Toast.LENGTH_LONG ).show();
mTts.speak("Hello", 0, null);
flag=1;
}
}
if(flag==1)
{
System.out.println("######"+msg);
mTts.speak(msg, 0, null);
}
}
@Override
public void onUtteranceCompleted(String uttId) {
stopSelf();
System.out.println("onUtteranceCompleted"+msg);
}
@Override
public void onDestroy() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
RECEIVER CLASS
public class SMSReceiver extends BroadcastReceiver
{
String name=null;
private Context mContext;
@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[]) pdus[n]);
}
// show first message
String sms1 = smsMessage[0].getMessageBody();
String from = smsMessage[0].getOriginatingAddress();
//String name = getDisplayNameFromPhoneNo( from);
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(from));
Cursor c = context.getContentResolver().query(lookupUri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
while(c.moveToNext()){
/* If we find a match we put it in a String.*/
name = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
}
Toast toast = Toast.makeText(context, "SMS Received from: " + from, Toast.LENGTH_LONG);
toast.show();
System.out.println("!!!!"+name);
Toast.makeText(context, "name: " + name, Toast.LENGTH_LONG).show();
//smsTalk.speakSMS(name);
//SMSTalk.mTts.speak("You have an SMS from "+name, 0, null);
context.startService(new Intent(context,SMSTalk.class));
SMSTalk smsTalk = new SMSTalk();
smsTalk.readName(name);
}
}
Your answer is probably there :
http://developer.android.com/guide/topics/fundamentals/services.html
Citations from the webpage :
Caution:
A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application’s main thread can remain dedicated to user interaction with your activities.
Should you use a service or a thread?
A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.
If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.
Remember that if you do use a service, it still runs in your application’s main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.