Após enviar o sms pelo Emulator Control, aparece a notificação no emulador e depois a app falha.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;
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();
Toast.makeText(context, " SMS de aluno "+telefone, Toast.LENGTH_LONG).show();
}
}
Erro
02-15 12:43:40.206: E/AndroidRuntime(1190): FATAL EXCEPTION: main
02-15 12:43:40.206: E/AndroidRuntime(1190): Process: br.com.caelum.cadastro, PID: 1190
02-15 12:43:40.206: E/AndroidRuntime(1190): java.lang.RuntimeException: Unable to instantiate receiver br.com.caelum.cadastro.SMSReceiver: java.lang.ClassNotFoundException: Didn't find class "br.com.caelum.cadastro.SMSReceiver" on path: DexPathList[[zip file "/data/app/br.com.caelum.cadastro-2.apk"],nativeLibraryDirectories=[/data/app-lib/br.com.caelum.cadastro-2, /system/lib]]
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2388)
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.app.ActivityThread.access$1700(ActivityThread.java:135)
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.os.Looper.loop(Looper.java:136)
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.app.ActivityThread.main(ActivityThread.java:5001)
02-15 12:43:40.206: E/AndroidRuntime(1190): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 12:43:40.206: E/AndroidRuntime(1190): at java.lang.reflect.Method.invoke(Method.java:515)
02-15 12:43:40.206: E/AndroidRuntime(1190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-15 12:43:40.206: E/AndroidRuntime(1190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-15 12:43:40.206: E/AndroidRuntime(1190): at dalvik.system.NativeStart.main(Native Method)
02-15 12:43:40.206: E/AndroidRuntime(1190): Caused by: java.lang.ClassNotFoundException: Didn't find class "br.com.caelum.cadastro.SMSReceiver" on path: DexPathList[[zip file "/data/app/br.com.caelum.cadastro-2.apk"],nativeLibraryDirectories=[/data/app-lib/br.com.caelum.cadastro-2, /system/lib]]
02-15 12:43:40.206: E/AndroidRuntime(1190): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-15 12:43:40.206: E/AndroidRuntime(1190): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
02-15 12:43:40.206: E/AndroidRuntime(1190): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
02-15 12:43:40.206: E/AndroidRuntime(1190): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
02-15 12:43:40.206: E/AndroidRuntime(1190): ... 10 more