0

I am trying to make SmsListener but the app breaks when I try to call tts

Below is the listener:

package com.example.hope_1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsListener extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;
        String msg_from;
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for(int i=0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    msg_from = msgs[i].getOriginatingAddress();
                    final String msgBody = msgs[i].getMessageBody();
                    Log.println(0, msg_from, msgBody);
                    Toast toast =  Toast.makeText(context, msg_from + "   " + msgBody,Toast.LENGTH_LONG);
                    toast.show();

                  //Application is crashing on line below
                    SmsSpeaker.tts.speak(msgBody,TextToSpeech.QUEUE_FLUSH, null);
                }
            }catch(Exception e){
                    Log.d("Exception caught",e.getMessage());
            }
        }
    }
}
}

Below is code for Sms Speaker

package com.example.hope_1;

import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class SmsSpeaker extends Service implements OnInitListener {

public static TextToSpeech tts;

public int onStartCommand(Intent intent,int flags,int startId){
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}
@Override
public void onInit(int status) {
    Toast toast; 
    // TODO Auto-generated method stub
    if(status == TextToSpeech.SUCCESS){
        tts.setLanguage(Locale.US);
        toast = Toast.makeText(SmsSpeaker.this, "TTS Started", Toast.LENGTH_LONG);
        toast.show();
        }
    else {
        toast = Toast.makeText(SmsSpeaker.this, TextToSpeech.ERROR, Toast.LENGTH_LONG);
        toast.show();
        toast = Toast.makeText(SmsSpeaker.this, "TTS Engine failed to start", Toast.LENGTH_LONG);
        toast.show();
    }

}


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

I tried using LogCat but that did not help...logcat is blank

The application is crashing without any exception

I tried to debug the code but on debug it shows "Source not found". I tried looking at other answers but it didn't help much.

Can you please tell me why the application is crashing.

4

1 回答 1

0

You did not initialize tts

public int onStartCommand(Intent intent,int flags,int startId){
super.onStartCommand(intent, flags, startId);
tts = new TextToSpeech(this, this);
return START_STICKY;

}

于 2013-03-31T00:01:31.393 回答