I'm trying to cope with SMS receiving funcions in Android. I read a lot of related topics on Stackoverflow and other sites and I tried with a very simple Class that simply print on the console that a message has been received and the message, but I cannot figure out why it doesn't work.
I see that similar questions remained unanswered in the past (see Android - Broadcast Receiver not being fired)
Hope someone could find where's the problem with my code.
Code:
package com.storassa.android.smsapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Intent recieved: " + intent.getAction());
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) {
System.out.println("Message recieved: " + messages[0].getMessageBody());
}
}
}
}
}
And Manifest
<manifest package="com.storassa.android.smsapp"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="6"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application android:label="@string/app_name" android:permission="android.permission.RECEIVE_SMS">
<receiver android:name="com.storassa.android.smsapp.SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>