Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro após enviar SMS

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
2 respostas

Oi Douglas,

olhando a stacktrace dá pra perceber direitinho o erro que está dando, olha só:

Unable to instantiate receiver br.com.caelum.cadastro.SMSReceiver: java.lang.ClassNotFoundException: Didn't find class "br.com.caelum.cadastro.SMSReceiver"

Ele está dizendo que não encontrou a classe br.com.caelum.cadastro.SMSReceiver

Isso pode acontecer quando você coloca a classe num pacote diferente do br.com.caelum.cadastro que você especificou no AndroidManifest.xml.

Para resolver isso, o jeito mais simples é colocar a classe SMSReceiver dentro do pacote br.com.caelum.cadastro.

Abraço.

solução!

Obrigado,

mudei o manifest, passando o pacote

<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>