0

I am developing an android application that receives SMS messages and uses this data inside the application. Because I want the SMS messages' BroadCastreceiver to close when I close the application, I have placed it inside the main activity:

public class App extends Activity {
 public class SmsReceiver extends BroadcastReceiver {
  ...
 }
 ...
}

It is added in the manifest:

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

The activity runs fine, until I receive an SMS message. I then get the following error:

10-23 15:01:50.019: E/AndroidRuntime(10161): java.lang.RuntimeException: Unable to instantiate receiver be.allys.msgwall.app.App.SmsReceiver: java.lang.ClassNotFoundException: be.allys.msgwall.app.App.SmsReceiver in loader dalvik.system.PathClassLoader[/data/app/be.allys.msgwall.app-1.apk]

Is this because the receiver class is inside another class? That would seem like a possible cause to me, if it weren't for the fact that another post on StackOverflow advised me to do it this way.

If that's not the reason, then what is? Any help would be greatly appreciated.

Neko
  • 3,550
  • 7
  • 28
  • 34

1 Answers1

3

When you register a receiver which is an inter class in the manifest,you should declare the receiver name like this OuterClass$interClass in the manifest.

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

EDIT 1: You should register the receiver dynamically, so you need have two methods like below code snippet, then you can call registerSMSReceiver in the onResume of Activity and call unRegisterSMSReceiver in the onPause. This will fix your problem.

private void registerSMSReceiver()
{
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(receiver, filter);
}

private void unRegisterSMSReceiver()
{
    unregisterReceiver(receiver);
}
dreamtale
  • 2,905
  • 1
  • 26
  • 38
  • This does not make the error go away yet. Any other ideas...? – Neko Oct 23 '12 at 14:12
  • @Neko Make the receiver to static class and try it again. – dreamtale Oct 23 '12 at 14:20
  • This works, but leaves me with the same problem I had before. I want the BroadcastReceiver to close whenever the activity closes, so that it will not fire up my application whenever I receive an sms message. – Neko Oct 23 '12 at 14:41
  • From what I've read, onDestroy() is sometimes not called when an activity goes away. You might be better off tying the registration and deregistration to the "resume" and "pause" events. The tradeoff being that you'll get more deregister/register pairs when the activity hasn't actually gone away. – David M. Karr Oct 23 '12 at 15:29
  • Where the register the receiver in the life cycle of the Activity depends on the what you want actually what code you wrote in the *onReceive*. For example, you will update UI in the onReceive method, then you need register the receiver between the onResume and onPause. – dreamtale Oct 23 '12 at 15:35
  • @DavidM.Karr Thanks for your attention,in his situation, onResume/onPause is a good choice. – dreamtale Oct 23 '12 at 15:43