Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate receiver in BroadcastReceiver SMS

Why I have this error :

ERROR/AndroidRuntime(854): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(854): java.lang.RuntimeException: Unable to instantiate receiver com.android.GPS21.SmsReceiver: java.lang.ClassNotFoundException: com.android.GPS21.SmsReceiver in loader dalvik.system.PathClassLoader@43d02ef0
ERROR/AndroidRuntime(854): Caused by: java.lang.ClassNotFoundException: com.android.GPS21.SmsReceiver in loader dalvik.system.PathClassLoader@43d02ef0

This is my onReceive events:

public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  Log.i(LOG_TAG, "Recieved a message");
  if (intent.getAction().equals(ACTION)) {
   // if(message starts with SMStretcher recognize BYTE)
   StringBuilder sb = new StringBuilder();

   // The SMS-Messages are 'hiding' within the extras of the Intent.
   Bundle bundle = intent.getExtras();
   if (bundle != null) {

    // Get all messages contained in the Intent
    // Telephony.Sms.Intents.getMessagesFromIntent(intent) does not
    // work anymore hence the below changes

    Object[] pduObj = (Object[]) bundle.get("pdus");
    SmsMessage[] messages = new SmsMessage[pduObj.length];
    for (int i = 0; i < pduObj.length; i++)
     messages[i] = SmsMessage.createFromPdu((byte[]) pduObj[i]);
    // Feed the StringBuilder with all Messages found.
    for (SmsMessage currentMessage : messages) {
     sb.append("SMS Received From: ");
     // Sender-Number
     sb.append(currentMessage.getDisplayOriginatingAddress());
     sb.append("\nMessage : ");
     // Actual Message-Content
     sb.append(currentMessage.getDisplayMessageBody());
    }
   }
   // Logger Debug-Output
   Log.i(LOG_TAG, "[SMSApp] onReceive: " + sb);

   // Show the Notification containing the Message.
   Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
  }

In debug that onReceive() is error.

I just make BroadcastReceiver to receive SMS and show in notification Toast..

And I try send SMS from DDMS and that error appear..

like image 330
fadli wdt Avatar asked Jan 06 '11 19:01

fadli wdt


5 Answers

Your manifest claims you have a class named com.android.GPS21.SmsReceiver, and Android cannot find it.

like image 54
CommonsWare Avatar answered Nov 08 '22 07:11

CommonsWare


This is an old question and I'm not sure that this was your trouble, but I just had this issue. Within Eclipse, I created the folder (really a package) in the wrong place. For example

Incorrect

enter image description here

The reason this is incorrect is that the Broadcast folder/package isn't within the namespace like you expect. This happens if you right click on My Program/src and create the package there. Notice that it is My Program/src/Broadcast which is what is wrong.

The reason is that the SmsReceiver class isn't in your namespace. In this case you may have something like this in your manifest.

<receiver android:name=".Broadcast.SmsReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

When this is triggered, the SmsReceiver class is NOT found and you'll get the error.

Correct

enter image description here

The correct way to do this, and get the package in the proper namespace is to right click on com.example.my.program and create it there instead. Notice that it is My Program/src/com.example.my.program.Broadcast which is now correctly in the same namespace.

This will be found in by the app with the same manifest code above.

like image 35
Kirk Avatar answered Nov 08 '22 08:11

Kirk


Your broadcastReceiver class must be a public class by example

public class ReceptorLlamadas extends BroadcastReceiver

like image 26
red_neo Avatar answered Nov 08 '22 09:11

red_neo


You have to write the complete path to your broadcastReceiver, I mean, if in your manifest file you jave package="com.myapp", but your MySmsBroadcast.java is not exactly under myapp package (it's on myapp.smsStuff.MySmsBroadcast, for example), in the

like image 28
lrr Avatar answered Nov 08 '22 09:11

lrr


I had the same problem as well. In my case, my Virtual Device was corrupted.

Try creating a new one and running with it. Worked for me!

like image 44
Ben Ripley Avatar answered Nov 08 '22 09:11

Ben Ripley