Q:

How to call TextToSpeech from Broadcast Receiver

I my app I'm calling a Broadcast Receiver to read incoming Text Messages and say it aloud. My Broadcast Receiver gets called properly, it reads the text message properly but when it comes to the speak() method, it just crashes. Here's my code:
This is the BroadcastReceiver:
public class DrivingModeSpeaker extends BroadcastReceiver  {

            @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        int n;
        Bundle bundle=intent.getExtras();
        Object messages[]=(Object[])bundle.get("pdus");
        SmsMessage smsMessage[]=new SmsMessage[messages.length];
        for(n=0;n<messages.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,"In DrivingModeSpeaker BR",1);
        toast.show();
        DrivingMode.speakSMS(sms1);

    }

}

And this is DrivingMode.java which contains the speak() method:
public class DrivingMode extends Activity {
    private static TextToSpeech myTts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.message_m);
        myTts = new TextToSpeech(this,ttsInitListener);

    }

private TextToSpeech.OnInitListener ttsInitListener=new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int version) {
            // myTts.speak(""+o, 0 ,null);

        }
    };
    public static void speakSMS(String sms)
    {
        myTts.speak(sms,0,null);

    }

}

I have tried out this code in a separate project, it works. But in my main project, it doesn't - could it be because of two broadcast receivers clashing or something? I dunno, I'm new to Android, please help!

A:

Currently you are trying to call Activity method by Creating an instance of Activity which through NullPointerException if Activity is not running. so instead of calling Activity's method start Activity from BroadcastReceiver and send sms data using Intent.putExtra as :
For Example :
public void onReceive(Context context, Intent intent) 
    {
       // Your code here....
        Toast toast=Toast.makeText(context,"In DrivingModeSpeaker BR",1);
        toast.show();
        // start Activity here
        Intent intent  = new Intent(context,
                    DrivingMode.class);
            intent.putExtra("sms", sms);  //<<< put sms text
            context.startActivity(intent);   

    }

and in DrivingMode onCreate method get sms data and call speakSMS method as :
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.message_m);
        Bundle extras = getIntent().getExtras();
        myTts = new TextToSpeech(this,ttsInitListener);
        DrivingMode.speakSMS(extras.getString("sms"));

    }

also use TextToSpeech.setOnUtteranceCompletedListener for finishing Activity  at the end of Speak

