Reputation: 3730
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.
Upvotes: 0
Views: 957
Reputation: 2913
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);
}
Upvotes: 3