Aparentemente parece esta tudo certo,
Suspeito que é o emulador
Fiz o teste no Emulator Control com um numero NÃO cadastro e só apareceu a mensagem e fiz o teste com numero cadastrado apareceu a mensagem e o toast, MAS não ouvi o som.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;
import android.widget.Toast;
import br.com.caelum.cadastro.dao.AlunoDAO;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[]mensagens =(Object[])intent.getExtras().get("pdus");
//ultima sms , de índice 0
byte[] mensagem=(byte[])mensagens[0];
// Classe que representa o SMS
SmsMessage sms = SmsMessage.createFromPdu(mensagem);
String telefone = sms.getDisplayOriginatingAddress();
AlunoDAO dao = new AlunoDAO(context);
if(dao.isAluno(telefone)){
Toast.makeText(context, " SMS de aluno "+telefone, Toast.LENGTH_LONG).show();
MediaPlayer mp = MediaPlayer.create(context,br.com.caelum.cadastro.R.raw.msg);
//mp.start();
}
dao.close();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.caelum.cadastro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".ListaAlunosActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FormularioActivity" >
</activity>
<activity
android:name=".DouglasActivity"
android:label="@string/title_activity_douglas" >
</activity>
<receiver android:name="br.com.caelum.cadastro.receiver.SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>