Tony Olendo
Tony Olendo

Reputation: 157

Android SMS Broadcast Receiver not working

I' m trying to build an app that receives notifications of incoming sms. I need help as incoming SMS's are not detected on any device. I am using the following code that I got from here:

http://karanbalkar.com/2014/09/display-incoming-sms-messages-in-android/

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class MySMSApp extends BroadcastReceiver {

public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if (intent.getAction().equals(ACTION)){ 
        Bundle bundle = intent.getExtras();
        if (bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++){
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            for (SmsMessage message : messages){

                String strMessageFrom = message.getDisplayOriginatingAddress();
                String strMessageBody = message.getDisplayMessageBody();

                Toast.makeText(context, "SMS Message received from:" +strMessageFrom, Toast.LENGTH_LONG).show();
                Toast.makeText(context, "SMS Message content" +strMessageBody, Toast.LENGTH_LONG).show();

            }
        }    
    } 
}

And the manifest reads as follows. I have setup the intent receiver to trigger the class above but it seems not to do so. I'm unable to telnet onto the emulator to spoof an incoming sms and so I'm blind as to what's going on.

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <receiver android:name=".SMSBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>
    </activity>
</application>

Upvotes: 0

Views: 1883

Answers (1)

Mattia Maestrini
Mattia Maestrini

Reputation: 32780

In AndroidManifest.xml you use SMSBroadcastReceiver, but your BroadcastReceiver class name is MySMSApp. Also your <receiver> element is inside the <activity> element.

Try to use this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".MySMSApp">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

Upvotes: 2

Related Questions