Possible Duplicate:
Android SMS Receiver not working
I am in the beginning stages of a simple app to intercept sms messages from a specific number. At this point, all I'm trying to achieve is a toast when the onReceive method is fired, however I'm not getting anything.
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.*****"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".Receiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
And the receiver
public class Receiver extends BroadcastReceiver {
private static final String SENDER = "****";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Received!", Toast.LENGTH_LONG).show();
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
Toast.makeText(context, "Received a message!", Toast.LENGTH_LONG).show();
//abortBroadcast();
}
}
}
}
}
Update 1 After creating an activity and launching the app, the receiver is registered. I need a way to have the receiver registered automatically though. There is no activity for the app, just the onReceive method intercepting the message. Is this possible? Perhaps using the boot complete intent?
UPdate 2 Solved it using a receiver for BOOT_COMPLETED to start a service which registered the sms receiver. Thanks.