How do I play a sound when my application receives a sms?
<p>I have used BroadcastReceiver class in my application to receive incoming sms and also give the high preference to my application in manifest file so that incoming message should be handled by my application first.</p>

<p>My main file is: </p>

<pre><code>package com.example.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;



public class Demo extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i&lt;msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            //   str += "SMS from " + msgs[i].getOriginatingAddress();                     
            //  str += " :";
            str += msgs[i].getMessageBody().toString();
            //   str += "\n";        
        }
        //---display the new SMS message---
        //Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        String[] msgText;
        String newstr="";
        msgText=str.split(",");
        newstr+=msgText[1]+","+msgText[2];
        if(msgText[0].equals("button"))
        {                
            Intent i = new Intent(context,Second.class);
            i.putExtra("msg",newstr);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
            context.startActivity(i);
        }
    }
    this.abortBroadcast();
}



}
</code></pre>

<p>And manifest file is: </p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo" android:versionCode="1"
android:versionName="1.0"&gt;
&lt;uses-sdk android:minSdkVersion="8" /&gt;

&lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
    &lt;activity android:name="Second" class=".second"
        android:label="Demo"&gt;
    &lt;/activity&gt;
    &lt;receiver android:name=".Demo"&gt;
        &lt;intent-filter android:priority="100"&gt;
            &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
&lt;/application&gt;
&lt;uses-permission android:name="android.permission.SEND_SMS"&gt;
&lt;/uses-permission&gt;
&lt;uses-permission android:name="android.permission.RECEIVE_SMS"&gt;
&lt;/uses-permission&gt;
&lt;/manifest&gt;
</code></pre>

<p>What I want to know is how can I play a sound when my application receives a sms? </p>

<p>I am confused about where should I implement the code because after analyzing the data I am switching to the second screen (if msg is in a particular format).</p>
